Search in sources :

Example 31 with PersistenceContext

use of org.hibernate.engine.spi.PersistenceContext in project hibernate-orm by hibernate.

the class TestAutoFlushBeforeQueryExecution method testAutoflushIsRequired.

@Test
public void testAutoflushIsRequired() {
    Session s = openSession();
    Transaction txn = s.beginTransaction();
    Publisher publisher = new Publisher();
    publisher.setName("name");
    s.save(publisher);
    assertTrue("autoflush entity create", s.createQuery("from Publisher p").list().size() == 1);
    publisher.setName("name");
    assertTrue("autoflush entity update", s.createQuery("from Publisher p where p.name='name'").list().size() == 1);
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    publisher = (Publisher) s.get(Publisher.class, publisher.getId());
    assertTrue(publisher.getAuthors().isEmpty());
    final PersistenceContext persistenceContext = ((SessionImplementor) s).getPersistenceContext();
    final ActionQueue actionQueue = ((SessionImpl) s).getActionQueue();
    assertEquals(1, persistenceContext.getCollectionEntries().size());
    assertEquals(1, persistenceContext.getCollectionsByKey().size());
    assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    Author author1 = new Author();
    author1.setPublisher(publisher);
    publisher.getAuthors().add(author1);
    assertTrue("autoflush collection update", s.createQuery("select a from Publisher p join p.authors a").list().size() == 1);
    assertEquals(2, persistenceContext.getCollectionEntries().size());
    assertEquals(2, persistenceContext.getCollectionsByKey().size());
    assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionEntries().containsKey(author1.getBooks()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(author1.getBooks()));
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    author1.setPublisher(null);
    s.delete(author1);
    publisher.getAuthors().clear();
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    assertTrue("autoflush collection update", s.createQuery("select a from Publisher p join p.authors a").list().size() == 0);
    assertEquals(1, persistenceContext.getCollectionEntries().size());
    assertEquals(1, persistenceContext.getCollectionsByKey().size());
    assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    Set<Author> authorsOld = publisher.getAuthors();
    publisher.setAuthors(new HashSet<Author>());
    Author author2 = new Author();
    author2.setName("author2");
    author2.setPublisher(publisher);
    publisher.getAuthors().add(author2);
    List results = s.createQuery("select a from Publisher p join p.authors a").list();
    assertEquals(1, results.size());
    assertEquals(2, persistenceContext.getCollectionEntries().size());
    assertEquals(2, persistenceContext.getCollectionsByKey().size());
    assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionEntries().containsKey(author2.getBooks()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(author2.getBooks()));
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    s.delete(publisher);
    assertTrue("autoflush delete", s.createQuery("from Publisher p").list().size() == 0);
    txn.commit();
    s.close();
}
Also used : ActionQueue(org.hibernate.engine.spi.ActionQueue) Transaction(org.hibernate.Transaction) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) List(java.util.List) SessionImpl(org.hibernate.internal.SessionImpl) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

PersistenceContext (org.hibernate.engine.spi.PersistenceContext)31 Serializable (java.io.Serializable)8 EntityEntry (org.hibernate.engine.spi.EntityEntry)8 EntityKey (org.hibernate.engine.spi.EntityKey)7 EventSource (org.hibernate.event.spi.EventSource)7 EntityPersister (org.hibernate.persister.entity.EntityPersister)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 List (java.util.List)5 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)5 AssertionFailure (org.hibernate.AssertionFailure)4 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)4 Type (org.hibernate.type.Type)4 ArrayList (java.util.ArrayList)3 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)3 StatefulPersistenceContext (org.hibernate.engine.internal.StatefulPersistenceContext)3 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)3 EventType (org.hibernate.event.spi.EventType)3 PostLoadEventListener (org.hibernate.event.spi.PostLoadEventListener)3 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)3 HibernateProxy (org.hibernate.proxy.HibernateProxy)3