use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class SessionImpl method getFilterQueryPlan.
private FilterQueryPlan getFilterQueryPlan(Object collection, String filter, QueryParameters parameters, boolean shallow) throws HibernateException {
if (collection == null) {
throw new NullPointerException("null collection passed to filter");
}
CollectionEntry entry = persistenceContext.getCollectionEntryOrNull(collection);
final CollectionPersister roleBeforeFlush = (entry == null) ? null : entry.getLoadedPersister();
FilterQueryPlan plan = null;
if (roleBeforeFlush == null) {
// if it was previously unreferenced, we need to flush in order to
// get its state into the database in order to execute query
flush();
entry = persistenceContext.getCollectionEntryOrNull(collection);
CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
if (roleAfterFlush == null) {
throw new QueryException("The collection was unreferenced");
}
plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleAfterFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
} else {
// otherwise, we only need to flush if there are in-memory changes
// to the queried tables
plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleBeforeFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
if (autoFlushIfRequired(plan.getQuerySpaces())) {
// might need to run a different filter entirely afterQuery the flush
// because the collection role may have changed
entry = persistenceContext.getCollectionEntryOrNull(collection);
CollectionPersister roleAfterFlush = (entry == null) ? null : entry.getLoadedPersister();
if (roleBeforeFlush != roleAfterFlush) {
if (roleAfterFlush == null) {
throw new QueryException("The collection was dereferenced");
}
plan = getFactory().getQueryPlanCache().getFilterQueryPlan(filter, roleAfterFlush.getRole(), shallow, getLoadQueryInfluencers().getEnabledFilters());
}
}
}
if (parameters != null) {
parameters.getPositionalParameterValues()[0] = entry.getLoadedKey();
parameters.getPositionalParameterTypes()[0] = entry.getLoadedPersister().getKeyType();
}
return plan;
}
use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class CollectionType method preserveSnapshot.
private void preserveSnapshot(PersistentCollection original, PersistentCollection result, Type elemType, Object owner, Map copyCache, SharedSessionContractImplementor session) {
Serializable originalSnapshot = original.getStoredSnapshot();
Serializable resultSnapshot = result.getStoredSnapshot();
Serializable targetSnapshot;
if (originalSnapshot instanceof List) {
targetSnapshot = new ArrayList(((List) originalSnapshot).size());
for (Object obj : (List) originalSnapshot) {
((List) targetSnapshot).add(elemType.replace(obj, null, session, owner, copyCache));
}
} else if (originalSnapshot instanceof Map) {
if (originalSnapshot instanceof SortedMap) {
targetSnapshot = new TreeMap(((SortedMap) originalSnapshot).comparator());
} else {
targetSnapshot = new HashMap(CollectionHelper.determineProperSizing(((Map) originalSnapshot).size()), CollectionHelper.LOAD_FACTOR);
}
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) originalSnapshot).entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
Object resultSnapshotValue = (resultSnapshot == null) ? null : ((Map<Object, Object>) resultSnapshot).get(key);
Object newValue = elemType.replace(value, resultSnapshotValue, session, owner, copyCache);
if (key == value) {
((Map) targetSnapshot).put(newValue, newValue);
} else {
((Map) targetSnapshot).put(key, newValue);
}
}
} else if (originalSnapshot instanceof Object[]) {
Object[] arr = (Object[]) originalSnapshot;
for (int i = 0; i < arr.length; i++) {
arr[i] = elemType.replace(arr[i], null, session, owner, copyCache);
}
targetSnapshot = originalSnapshot;
} else {
// retain the same snapshot
targetSnapshot = resultSnapshot;
}
CollectionEntry ce = session.getPersistenceContext().getCollectionEntry(result);
if (ce != null) {
ce.resetStoredSnapshot(result, targetSnapshot);
}
}
use of org.hibernate.engine.spi.CollectionEntry in project hibernate-orm by hibernate.
the class UnversionedCascadeDereferencedCollectionTest method testMergeNullCollection.
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testMergeNullCollection() {
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.merge(one);
// afterQuery merging, one.getManies() should still be null;
// the EntityEntry loaded state should contain a PersistentCollection though.
assertNull(one.getManies());
eeOne = getEntityEntry(s, one);
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue("manies");
assertNotNull(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());
s.flush();
// Ensure the same EntityEntry is being used.
assertSame(eeOne, getEntityEntry(s, one));
// Ensure one.getManies() is still null.
assertNull(one.getManies());
// 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());
// Ensure eeOne.getLoadedState() returns null for collection afterQuery flush.
assertNull(eeOne.getLoadedValue("manies"));
// 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 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 testGetAndNullifyCollection.
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndNullifyCollection() {
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 ceManies = getCollectionEntry(s, maniesEEOneStateOrig);
assertNotNull(ceManies);
assertEquals(role, ceManies.getRole());
assertSame(sessionFactory().getCollectionPersister(role), ceManies.getLoadedPersister());
assertEquals(one.getId(), ceManies.getKey());
// nullify collection
one.setManies(null);
s.flush();
// Ensure the same EntityEntry is being used.
assertSame(eeOne, getEntityEntry(s, one));
// Ensure one.getManies() is still null.
assertNull(one.getManies());
// 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(ceManies.getRole());
assertNull(ceManies.getLoadedPersister());
assertNull(ceManies.getKey());
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull(maniesEEOneStateOrig.getKey());
assertNull(maniesEEOneStateOrig.getRole());
// Ensure eeOne.getLoadedState() returns null for collection afterQuery flush.
assertNull(eeOne.getLoadedValue("manies"));
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull(maniesEEOneStateOrig.getSession());
s.getTransaction().commit();
s.close();
}
Aggregations