public T Get(int id) { using (var session = SessionFactorySingleton.OpenSession()) { return(session.Get <T>(id)); } }
public IList <T> GetAll() { using (var session = SessionFactorySingleton.OpenSession()) { return(session.CreateCriteria <T>().List <T>()); } }
public long GetTotalReviewCount() { using (var session = SessionFactorySingleton.OpenSession()) { return(session.CreateQuery("select count(r.Id) from Review r") .UniqueResult <long>()); } }
public static void ClearDatabase() { using (var session = SessionFactorySingleton.OpenSession()) { session.CreateQuery("delete Review").ExecuteUpdate(); session.CreateQuery("delete User").ExecuteUpdate(); } }
public void Save(T entity) { using (var session = SessionFactorySingleton.OpenSession()) using (var transaction = session.BeginTransaction()) { session.SaveOrUpdate(entity); transaction.Commit(); } }
public void Delete(int id) { using (var session = SessionFactorySingleton.OpenSession()) using (var transaction = session.BeginTransaction()) { var proxy = session.Load <User>(id); session.Delete(proxy); transaction.Commit(); } }
public long GetReviewCountForUser(int userId) { using (var session = SessionFactorySingleton.OpenSession()) { return(session.CreateQuery( "select count(review.Id) from User user left join user.Reviews review " + "where user.Id = :userId") .SetInt32("userId", userId) .UniqueResult <long>()); } }
public IList <User> GetUsersWithReviewsWithRating_QueryOver(int rating) { using (var session = SessionFactorySingleton.OpenSession()) { return(session.QueryOver <User>() .JoinQueryOver(u => u.Reviews) .Where(r => r.Rating == rating) .TransformUsing(Transformers.DistinctRootEntity) .List <User>()); } }
public IList <User> GetUsersWithReviewsWithRating(int rating) { using (var session = SessionFactorySingleton.OpenSession()) { return(session.CreateQuery( "select u from User u join u.Reviews r where r.Rating = :rating") .SetInt32("rating", rating) .SetResultTransformer(Transformers.DistinctRootEntity) .List <User>()); } }
public void Can_write_and_read_user() { using (var session = SessionFactorySingleton.OpenSession()) { new PersistenceSpecification <User>(session) .CheckProperty(x => x.Username, "username") .CheckProperty(x => x.Password, "password") .CheckProperty(x => x.LastLoginDate, new DateTime(2011, 1, 2, 3, 4, 5)) .CheckList(x => x.Reviews, _reviews, new EntityIdComparer(), (u, r) => u.AddReview(r)); } }
public void Save(IEnumerable <T> entities) { using (var session = SessionFactorySingleton.OpenSession()) using (var transaction = session.BeginTransaction()) { foreach (var entity in entities) { session.SaveOrUpdate(entity); } transaction.Commit(); } }
private void SaveAllUsers() { using (var session = SessionFactorySingleton.OpenSession()) using (var transaction = session.BeginTransaction()) { session.Save(_user1); session.Save(_user2); session.Save(_user3); session.Save(_user4); transaction.Commit(); } }
private static void PrintReviewCountsWithLazyLoading() { WriteConsoleSeparator(); using (var session = SessionFactorySingleton.OpenSession()) { var users = session.CreateQuery("from User").List <User>(); foreach (var user in users) { System.Console.WriteLine(user.Username + ": " + user.Reviews.Count()); } } }
private static void PrintReviewCountsWithEagerLoading() { WriteConsoleSeparator(); using (var session = SessionFactorySingleton.OpenSession()) { var users = session.CreateQuery("from User user join fetch user.Reviews") .SetResultTransformer(Transformers.DistinctRootEntity) .List <User>(); foreach (var user in users) { System.Console.WriteLine(user.Username + ": " + user.Reviews.Count()); } } }
private static void CreateTestData() { using (var session = SessionFactorySingleton.OpenSession()) using (var transaction = session.BeginTransaction()) { var user1 = new User { Username = "******", Password = "******", LastLoginDate = new DateTime(2011, 1, 2, 3, 4, 5), }; var user2 = new User { Username = "******", Password = "******", LastLoginDate = new DateTime(2022, 2, 2, 3, 4, 5), }; var user3 = new User { Username = "******", Password = "******", LastLoginDate = new DateTime(3033, 3, 3, 3, 4, 5), }; user1.AddReview(new Review { Title = "User 1's first review", Body = "This is the first review written by User 1", DateTime = new DateTime(2011, 2, 3, 4, 5, 6), Rating = 5 }); user1.AddReview(new Review { Title = "User 1's second review", Body = "This is the second review written by User 1", DateTime = new DateTime(2011, 2, 3, 6, 7, 8), Rating = 4 }); user2.AddReview(new Review { Title = "User 2's first review", Body = "This is the first review written by User 2", DateTime = new DateTime(2011, 2, 4, 5, 6, 7), Rating = 5 }); user2.AddReview(new Review { Title = "User 2's second review", Body = "This is the second review written by User 2", DateTime = new DateTime(2022, 2, 4, 6, 7, 8), Rating = 4 }); user2.AddReview(new Review { Title = "User 2's third review", Body = "This is the third review written by User 2", DateTime = new DateTime(2022, 2, 4, 7, 8, 9), Rating = 3 }); user3.AddReview(new Review { Title = "User 3's first review", Body = "This is the first review written by User 3", DateTime = new DateTime(3011, 3, 4, 5, 6, 7), Rating = 1 }); session.Save(user1); session.Save(user2); session.Save(user3); transaction.Commit(); } }