Search in sources :

Example 1 with ActionQueue

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

the class AbstractFlushingEventListener method flushCollections.

/**
	 * process any unreferenced collections and then inspect all known collections,
	 * scheduling creates/removes/updates
	 */
@SuppressWarnings("unchecked")
private int flushCollections(final EventSource session, final PersistenceContext persistenceContext) throws HibernateException {
    LOG.trace("Processing unreferenced collections");
    final Map.Entry<PersistentCollection, CollectionEntry>[] entries = IdentityMap.concurrentEntries((Map<PersistentCollection, CollectionEntry>) persistenceContext.getCollectionEntries());
    final int count = entries.length;
    for (Map.Entry<PersistentCollection, CollectionEntry> me : entries) {
        CollectionEntry ce = me.getValue();
        if (!ce.isReached() && !ce.isIgnore()) {
            Collections.processUnreachableCollection(me.getKey(), session);
        }
    }
    // Schedule updates to collections:
    LOG.trace("Scheduling collection removes/(re)creates/updates");
    ActionQueue actionQueue = session.getActionQueue();
    for (Map.Entry<PersistentCollection, CollectionEntry> me : IdentityMap.concurrentEntries((Map<PersistentCollection, CollectionEntry>) persistenceContext.getCollectionEntries())) {
        PersistentCollection coll = me.getKey();
        CollectionEntry ce = me.getValue();
        if (ce.isDorecreate()) {
            session.getInterceptor().onCollectionRecreate(coll, ce.getCurrentKey());
            actionQueue.addAction(new CollectionRecreateAction(coll, ce.getCurrentPersister(), ce.getCurrentKey(), session));
        }
        if (ce.isDoremove()) {
            session.getInterceptor().onCollectionRemove(coll, ce.getLoadedKey());
            actionQueue.addAction(new CollectionRemoveAction(coll, ce.getLoadedPersister(), ce.getLoadedKey(), ce.isSnapshotEmpty(coll), session));
        }
        if (ce.isDoupdate()) {
            session.getInterceptor().onCollectionUpdate(coll, ce.getLoadedKey());
            actionQueue.addAction(new CollectionUpdateAction(coll, ce.getLoadedPersister(), ce.getLoadedKey(), ce.isSnapshotEmpty(coll), session));
        }
        // todo : I'm not sure the !wasInitialized part should really be part of this check
        if (!coll.wasInitialized() && coll.hasQueuedOperations()) {
            actionQueue.addAction(new QueuedOperationCollectionAction(coll, ce.getLoadedPersister(), ce.getLoadedKey(), session));
        }
    }
    actionQueue.sortCollectionActions();
    return count;
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) ActionQueue(org.hibernate.engine.spi.ActionQueue) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) EntityEntry(org.hibernate.engine.spi.EntityEntry) CollectionEntry(org.hibernate.engine.spi.CollectionEntry) QueuedOperationCollectionAction(org.hibernate.action.internal.QueuedOperationCollectionAction) CollectionRecreateAction(org.hibernate.action.internal.CollectionRecreateAction) CollectionRemoveAction(org.hibernate.action.internal.CollectionRemoveAction) CollectionUpdateAction(org.hibernate.action.internal.CollectionUpdateAction) Map(java.util.Map) IdentityMap(org.hibernate.internal.util.collections.IdentityMap) CascadePoint(org.hibernate.engine.internal.CascadePoint)

Example 2 with ActionQueue

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

the class TestAutoFlushBeforeQueryExecution method testAutoflushIsNotRequiredWithUnrelatedCollectionChange.

@Test
public void testAutoflushIsNotRequiredWithUnrelatedCollectionChange() {
    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);
    UnrelatedEntity unrelatedEntity = new UnrelatedEntity();
    s.save(unrelatedEntity);
    txn.commit();
    s.close();
    s = openSession();
    txn = s.beginTransaction();
    unrelatedEntity = (UnrelatedEntity) s.get(UnrelatedEntity.class, unrelatedEntity.getId());
    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(s.createQuery("from UnrelatedEntity").list().size() == 1);
    assertEquals(2, persistenceContext.getCollectionEntries().size());
    assertEquals(1, persistenceContext.getCollectionsByKey().size());
    assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionEntries().containsKey(author1.getBooks()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    author1.setPublisher(null);
    s.delete(author1);
    publisher.getAuthors().clear();
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    assertTrue(s.createQuery("from UnrelatedEntity").list().size() == 1);
    assertEquals(2, persistenceContext.getCollectionEntries().size());
    assertEquals(1, persistenceContext.getCollectionsByKey().size());
    assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionEntries().containsKey(author1.getBooks()));
    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("from UnrelatedEntity").list();
    assertEquals(1, results.size());
    assertEquals(4, persistenceContext.getCollectionEntries().size());
    assertEquals(1, persistenceContext.getCollectionsByKey().size());
    assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
    assertTrue(persistenceContext.getCollectionEntries().containsKey(author2.getBooks()));
    assertTrue(persistenceContext.getCollectionEntries().containsKey(authorsOld));
    assertTrue(persistenceContext.getCollectionEntries().containsKey(author1.getBooks()));
    assertTrue(persistenceContext.getCollectionsByKey().values().contains(authorsOld));
    assertEquals(0, actionQueue.numberOfCollectionRemovals());
    s.flush();
    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 UnrelatedEntity").list().size() == 1);
    s.delete(unrelatedEntity);
    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)

Example 3 with ActionQueue

use of org.hibernate.engine.spi.ActionQueue 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

ActionQueue (org.hibernate.engine.spi.ActionQueue)3 List (java.util.List)2 Session (org.hibernate.Session)2 Transaction (org.hibernate.Transaction)2 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)2 SessionImpl (org.hibernate.internal.SessionImpl)2 Test (org.junit.Test)2 Map (java.util.Map)1 CollectionRecreateAction (org.hibernate.action.internal.CollectionRecreateAction)1 CollectionRemoveAction (org.hibernate.action.internal.CollectionRemoveAction)1 CollectionUpdateAction (org.hibernate.action.internal.CollectionUpdateAction)1 QueuedOperationCollectionAction (org.hibernate.action.internal.QueuedOperationCollectionAction)1 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)1 CascadePoint (org.hibernate.engine.internal.CascadePoint)1 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)1 EntityEntry (org.hibernate.engine.spi.EntityEntry)1 IdentityMap (org.hibernate.internal.util.collections.IdentityMap)1