/// <summary> /// Get a Hibernate Session, either from the current transaction or /// a new one. The latter is only allowed if "allowCreate" is true. /// </summary> /// <remarks>Note that this is not meant to be invoked from HibernateTemplate code /// but rather just in plain Hibernate code. Either rely on a thread-bound /// Session (via HibernateInterceptor), or use it in combination with /// ReleaseSession. /// <para> /// In general, it is recommended to use HibernateTemplate, either with /// the provided convenience operations or with a custom HibernateCallback /// that provides you with a Session to work on. HibernateTemplate will care /// for all resource management and for proper exception conversion. /// </para> /// </remarks> /// <param name="allowCreate"> if a non-transactional Session should be created when no /// transactional Session can be found for the current thread /// </param> /// <returns>Hibernate session.</returns> /// <exception cref="DataAccessResourceFailureException"> /// If the Session couldn't be created /// </exception> /// <exception cref="InvalidOperationException"> /// if no thread-bound Session found and allowCreate false /// </exception> /// <seealso cref="ReleaseSession"/> protected ISession DoGetSession(bool allowCreate) { return(!allowCreate ? SessionFactoryUtils.GetSession(SessionFactory, false) : SessionFactoryUtils.GetSession( SessionFactory, this.hibernateTemplate.EntityInterceptor, this.hibernateTemplate.AdoExceptionTranslator)); }
public void CustomerDaoTests() { Assert.AreEqual(91, customerDao.GetAll().Count); Customer c = new Customer(new DefaultCustomerClassificationCalculator()); c.Id = "MPOLL"; c.CompanyName = "Interface21"; customerDao.Save(c); c = customerDao.Get("MPOLL"); Assert.AreEqual(c.Id, "MPOLL"); Assert.AreEqual(c.CompanyName, "Interface21"); //Without flushing, nothing changes in the database: int customerCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers"); Assert.AreEqual(91, customerCount); //Flush the session to execute sql in the db. SessionFactoryUtils.GetSession(sessionFactory, true).Flush(); //Now changes are visible outside the session but within the same database transaction customerCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers"); Assert.AreEqual(92, customerCount); Assert.AreEqual(92, customerDao.GetAll().Count); c.CompanyName = "SpringSource"; customerDao.Update(c); c = customerDao.Get("MPOLL"); Assert.AreEqual(c.Id, "MPOLL"); Assert.AreEqual(c.CompanyName, "SpringSource"); customerDao.Delete(c); SessionFactoryUtils.GetSession(sessionFactory, true).Flush(); customerCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Customers"); Assert.AreEqual(92, customerCount); try { c = customerDao.Get("MPOLL"); Assert.Fail("Should have thrown HibernateObjectRetrievalFailureException when finding customer with Id = MPOLL"); } catch (HibernateObjectRetrievalFailureException e) { Assert.AreEqual("Customer", e.PersistentClassName); } }
public void ProductDaoTests() { Assert.AreEqual(830, orderDao.GetAll().Count); Order order = new Order(); Customer customer = customerDao.Get("PICCO"); order.Customer = customer; order.ShipCity = "New York"; orderDao.Save(order); int orderId = order.Id; order = orderDao.Get(orderId); Assert.AreEqual("PICCO", order.Customer.Id); Assert.AreEqual("New York", order.ShipCity); //SessionFactoryUtils.GetSession(sessionFactory, true).Flush(); // Required trip to db to get idendity column, so data is visible int ordersCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Orders"); Assert.AreEqual(831, ordersCount); Assert.AreEqual(831, orderDao.GetAll().Count); order.ShipCity = "Sao Paulo"; orderDao.Update(order); order = orderDao.Get(orderId); Assert.AreEqual("PICCO", order.Customer.Id); Assert.AreEqual("Sao Paulo", order.ShipCity); orderDao.Delete(order); //Without flushing, nothing changes in the database: SessionFactoryUtils.GetSession(sessionFactory, true).Flush(); ordersCount = (int)AdoTemplate.ExecuteScalar(CommandType.Text, "select count(*) from Orders"); Assert.AreEqual(830, ordersCount); try { order = orderDao.Get(orderId); Assert.Fail("Should have thrown HibernateObjectRetrievalFailureException when finding order with Id = " + orderId); } catch (HibernateObjectRetrievalFailureException e) { Assert.AreEqual("Order", e.PersistentClassName); } }
private ISession GetSession() { return(SessionFactoryUtils.GetSession(sessionFactory, true)); }