use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class UnversionedCascadeDereferencedCollectionTest method testGetAndReplaceCollection.
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndReplaceCollection() {
Session s = openSession();
s.getTransaction().begin();
UnversionedCascadeOne one = new UnversionedCascadeOne();
assertNull(one.getManies());
s.save(one);
assertNull(one.getManies());
EntityEntry eeOne = getEntityEntry(s, one);
assertNull(eeOne.getLoadedValue("manies"));
s.flush();
assertNull(one.getManies());
assertNull(eeOne.getLoadedValue("manies"));
s.getTransaction().commit();
s.close();
final String role = UnversionedCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (UnversionedCascadeOne) s.get(UnversionedCascadeOne.class, one.getId());
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
eeOne = getEntityEntry(s, one);
assertNotNull(one.getManies());
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue("manies");
assertSame(one.getManies(), maniesEEOneStateOrig);
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals(role, maniesEEOneStateOrig.getRole());
assertEquals(one.getId(), maniesEEOneStateOrig.getKey());
assertSame(s, maniesEEOneStateOrig.getSession());
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry(s, maniesEEOneStateOrig);
assertNotNull(ceManiesOrig);
assertEquals(role, ceManiesOrig.getRole());
assertSame(sessionFactory().getCollectionPersister(role), ceManiesOrig.getLoadedPersister());
assertEquals(one.getId(), ceManiesOrig.getKey());
// replace collection
one.setManies(new HashSet<Many>());
s.flush();
// Ensure the same EntityEntry is being used.
assertSame(eeOne, getEntityEntry(s, one));
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull(getCollectionEntry(s, maniesEEOneStateOrig));
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull(ceManiesOrig.getRole());
assertNull(ceManiesOrig.getLoadedPersister());
assertNull(ceManiesOrig.getKey());
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull(maniesEEOneStateOrig.getKey());
assertNull(maniesEEOneStateOrig.getRole());
// one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
assertTrue(PersistentCollection.class.isInstance(one.getManies()));
assertEquals(role, ((PersistentCollection) one.getManies()).getRole());
assertEquals(one.getId(), ((PersistentCollection) one.getManies()).getKey());
assertSame(s, ((AbstractPersistentCollection) one.getManies()).getSession());
// Ensure eeOne.getLoadedState() contains the new collection.
assertSame(one.getManies(), eeOne.getLoadedValue("manies"));
// Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
CollectionEntry ceManiesAfterReplace = getCollectionEntry(s, (PersistentCollection) one.getManies());
assertNotNull(ceManiesAfterReplace);
assertEquals(role, ceManiesAfterReplace.getRole());
assertSame(sessionFactory().getCollectionPersister(role), ceManiesAfterReplace.getLoadedPersister());
assertEquals(one.getId(), ceManiesAfterReplace.getKey());
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull(maniesEEOneStateOrig.getSession());
s.getTransaction().commit();
s.close();
}
use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class VersionedNoCascadeDereferencedCollectionTest method testGetAndReplaceCollection.
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndReplaceCollection() {
Session s = openSession();
s.getTransaction().begin();
VersionedNoCascadeOne one = new VersionedNoCascadeOne();
assertNull(one.getManies());
s.save(one);
assertNull(one.getManies());
EntityEntry eeOne = getEntityEntry(s, one);
assertNull(eeOne.getLoadedValue("manies"));
s.flush();
assertNull(one.getManies());
assertNull(eeOne.getLoadedValue("manies"));
s.getTransaction().commit();
s.close();
final String role = VersionedNoCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (VersionedNoCascadeOne) s.get(VersionedNoCascadeOne.class, one.getId());
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
eeOne = getEntityEntry(s, one);
assertNotNull(one.getManies());
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue("manies");
assertSame(one.getManies(), maniesEEOneStateOrig);
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals(role, maniesEEOneStateOrig.getRole());
assertEquals(one.getId(), maniesEEOneStateOrig.getKey());
assertSame(s, maniesEEOneStateOrig.getSession());
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry(s, maniesEEOneStateOrig);
assertNotNull(ceManiesOrig);
assertEquals(role, ceManiesOrig.getRole());
assertSame(sessionFactory().getCollectionPersister(role), ceManiesOrig.getLoadedPersister());
assertEquals(one.getId(), ceManiesOrig.getKey());
// replace collection
one.setManies(new HashSet<Many>());
s.flush();
// Ensure the same EntityEntry is being used.
assertSame(eeOne, getEntityEntry(s, one));
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull(getCollectionEntry(s, maniesEEOneStateOrig));
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull(ceManiesOrig.getRole());
assertNull(ceManiesOrig.getLoadedPersister());
assertNull(ceManiesOrig.getKey());
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull(maniesEEOneStateOrig.getKey());
assertNull(maniesEEOneStateOrig.getRole());
// one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
assertTrue(PersistentCollection.class.isInstance(one.getManies()));
assertEquals(role, ((PersistentCollection) one.getManies()).getRole());
assertEquals(one.getId(), ((PersistentCollection) one.getManies()).getKey());
assertSame(s, ((AbstractPersistentCollection) one.getManies()).getSession());
// Ensure eeOne.getLoadedState() contains the new collection.
assertSame(one.getManies(), eeOne.getLoadedValue("manies"));
// Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
CollectionEntry ceManiesAfterReplace = getCollectionEntry(s, (PersistentCollection) one.getManies());
assertNotNull(ceManiesAfterReplace);
assertEquals(role, ceManiesAfterReplace.getRole());
assertSame(sessionFactory().getCollectionPersister(role), ceManiesAfterReplace.getLoadedPersister());
assertEquals(one.getId(), ceManiesAfterReplace.getKey());
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull(maniesEEOneStateOrig.getSession());
s.getTransaction().commit();
s.close();
}
use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class MultipleSessionCollectionWarningTest method testSetCurrentSessionOverwritesNonConnectedSesssion.
@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testSetCurrentSessionOverwritesNonConnectedSesssion() {
Parent p = new Parent();
Child c = new Child();
p.children.add(c);
Session s1 = openSession();
s1.getTransaction().begin();
s1.saveOrUpdate(p);
// Now remove the collection from the PersistenceContext without unsetting its session
// This should never be done in practice; it is done here only to test that the warning
// gets logged. s1 will not function properly so the transaction will ultimately need
// to be rolled-back.
CollectionEntry ce = (CollectionEntry) ((SessionImplementor) s1).getPersistenceContext().getCollectionEntries().remove(p.children);
assertNotNull(ce);
// the collection session should still be s1; the collection is no longer "connected" because its
// CollectionEntry has been removed.
assertSame(s1, ((AbstractPersistentCollection) p.children).getSession());
Session s2 = openSession();
s2.getTransaction().begin();
Triggerable triggerable = logInspection.watchForLogMessages("HHH000470:");
assertFalse(triggerable.wasTriggered());
// The following should trigger warning because we're setting a new session when the collection already
// has a non-null session (and the collection is not "connected" to that session);
// Since s1 was not flushed, the collection role will not be known (no way to test that other than inspection).
s2.saveOrUpdate(p);
assertTrue(triggerable.wasTriggered());
// collection's session should be overwritten with s2
assertSame(s2, ((AbstractPersistentCollection) p.children).getSession());
s2.getTransaction().rollback();
s2.close();
s1.getTransaction().rollback();
s1.close();
}
use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class MultipleSessionCollectionWarningTest method testSetCurrentSessionOverwritesNonConnectedSesssionFlushed.
@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testSetCurrentSessionOverwritesNonConnectedSesssionFlushed() {
Parent p = new Parent();
Child c = new Child();
p.children.add(c);
Session s1 = openSession();
s1.getTransaction().begin();
s1.saveOrUpdate(p);
// flush the session so that p.children will contain its role
s1.flush();
// Now remove the collection from the PersistenceContext without unsetting its session
// This should never be done in practice; it is done here only to test that the warning
// gets logged. s1 will not function properly so the transaction will ultimately need
// to be rolled-back.
CollectionEntry ce = (CollectionEntry) ((SessionImplementor) s1).getPersistenceContext().getCollectionEntries().remove(p.children);
assertNotNull(ce);
// the collection session should still be s1; the collection is no longer "connected" because its
// CollectionEntry has been removed.
assertSame(s1, ((AbstractPersistentCollection) p.children).getSession());
Session s2 = openSession();
s2.getTransaction().begin();
Triggerable triggerable = logInspection.watchForLogMessages("HHH000470:");
assertFalse(triggerable.wasTriggered());
// The following should trigger warning because we're setting a new session when the collection already
// has a non-null session (and the collection is not "connected" to that session);
// The collection role and key should be included in the message (no way to test that other than inspection).
s2.saveOrUpdate(p);
assertTrue(triggerable.wasTriggered());
// collection's session should be overwritten with s2
assertSame(s2, ((AbstractPersistentCollection) p.children).getSession());
s2.getTransaction().rollback();
s2.close();
s1.getTransaction().rollback();
s1.close();
}
use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class MultipleCollectionListeners method addEvent.
public void addEvent(AbstractCollectionEvent event, Listener listener) {
CollectionEntry collectionEntry = event.getSession().getPersistenceContext().getCollectionEntry(event.getCollection());
Serializable snapshot = collectionEntry.getSnapshot();
log.debug("add Event: " + event.getClass() + "; listener = " + listener.getClass() + "; snapshot = " + snapshot);
listenersCalled.add(listener);
events.add(event);
snapshots.add(snapshot);
}
Aggregations