use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.
the class LoadPlanStructureAssertionTest method testEncapsulatedCompositeIdWithFetches2.
@Test
public void testEncapsulatedCompositeIdWithFetches2() {
Configuration cfg = new Configuration();
cfg.addAnnotatedClass(Card.class);
cfg.addAnnotatedClass(CardField.class);
cfg.addAnnotatedClass(Key.class);
cfg.addAnnotatedClass(PrimaryKey.class);
final SessionFactoryImplementor sf = (SessionFactoryImplementor) cfg.buildSessionFactory();
try {
final OuterJoinLoadable cardPersister = (OuterJoinLoadable) sf.getClassMetadata(Card.class);
doCompare(sf, cardPersister);
final LoadPlan cardLoadPlan = LoadPlanStructureAssertionHelper.INSTANCE.buildLoadPlan(sf, cardPersister);
assertEquals(LoadPlan.Disposition.ENTITY_LOADER, cardLoadPlan.getDisposition());
assertEquals(1, cardLoadPlan.getReturns().size());
// Check the root EntityReturn(Card)
final EntityReturn cardReturn = assertTyping(EntityReturn.class, cardLoadPlan.getReturns().get(0));
assertFalse(cardReturn.getIdentifierDescription().hasFetches());
// Card should have one fetch, the fields collection
assertEquals(1, cardReturn.getFetches().length);
final CollectionAttributeFetch fieldsFetch = assertTyping(CollectionAttributeFetch.class, cardReturn.getFetches()[0]);
assertNotNull(fieldsFetch.getElementGraph());
// the Card.fields collection has entity elements of type CardField...
final CollectionFetchableElementEntityGraph cardFieldElementGraph = assertTyping(CollectionFetchableElementEntityGraph.class, fieldsFetch.getElementGraph());
// CardField should have no fetches
assertEquals(0, cardFieldElementGraph.getFetches().length);
// But it should have 1 key-many-to-one fetch for Key (Card is already handled)
assertTrue(cardFieldElementGraph.getIdentifierDescription().hasFetches());
final FetchSource cardFieldElementGraphIdAsFetchSource = assertTyping(FetchSource.class, cardFieldElementGraph.getIdentifierDescription());
assertEquals(1, cardFieldElementGraphIdAsFetchSource.getFetches().length);
assertEquals(1, cardFieldElementGraphIdAsFetchSource.getBidirectionalEntityReferences().length);
BidirectionalEntityReference circularCardFetch = assertTyping(BidirectionalEntityReference.class, cardFieldElementGraphIdAsFetchSource.getBidirectionalEntityReferences()[0]);
assertSame(circularCardFetch.getTargetEntityReference(), cardReturn);
// the fetch above is to the other key-many-to-one for CardField.primaryKey composite: key
EntityFetch keyFetch = assertTyping(EntityFetch.class, cardFieldElementGraphIdAsFetchSource.getFetches()[0]);
assertEquals(Key.class.getName(), keyFetch.getEntityPersister().getEntityName());
} finally {
sf.close();
}
}
use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.
the class LoadPlanStructureAssertionTest method testManyToMany.
@Test
public void testManyToMany() {
Configuration cfg = new Configuration();
cfg.addResource("org/hibernate/test/immutable/entitywithmutablecollection/inverse/ContractVariation.hbm.xml");
SessionFactoryImplementor sf = (SessionFactoryImplementor) cfg.buildSessionFactory();
try {
doCompare(sf, (OuterJoinLoadable) sf.getClassMetadata(org.hibernate.test.immutable.entitywithmutablecollection.Contract.class));
} finally {
sf.close();
}
}
use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.
the class LoadPlanStructureAssertionTest method testEncapsulatedCompositeIdNoFetches1.
@Test
public void testEncapsulatedCompositeIdNoFetches1() {
// CardField is an entity with a composite identifier mapped via a @EmbeddedId class (CardFieldPK) defining
// a @ManyToOne
Configuration cfg = new Configuration();
cfg.addAnnotatedClass(EncapsulatedCompositeIdResultSetProcessorTest.CardField.class);
cfg.addAnnotatedClass(EncapsulatedCompositeIdResultSetProcessorTest.Card.class);
SessionFactoryImplementor sf = (SessionFactoryImplementor) cfg.buildSessionFactory();
try {
doCompare(sf, (OuterJoinLoadable) sf.getClassMetadata(EncapsulatedCompositeIdResultSetProcessorTest.CardField.class));
doCompare(sf, (OuterJoinLoadable) sf.getClassMetadata(EncapsulatedCompositeIdResultSetProcessorTest.Card.class));
} finally {
sf.close();
}
}
use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.
the class CollectionCacheInvalidator method evictCache.
private void evictCache(Object entity, EntityPersister persister, EventSource session, Object[] oldState) {
try {
SessionFactoryImplementor factory = persister.getFactory();
Set<String> collectionRoles = factory.getMetamodel().getCollectionRolesByEntityParticipant(persister.getEntityName());
if (collectionRoles == null || collectionRoles.isEmpty()) {
return;
}
for (String role : collectionRoles) {
final CollectionPersister collectionPersister = factory.getMetamodel().collectionPersister(role);
if (!collectionPersister.hasCache()) {
// ignore collection if no caching is used
continue;
}
// this is the property this OneToMany relation is mapped by
String mappedBy = collectionPersister.getMappedByProperty();
if (!collectionPersister.isManyToMany() && mappedBy != null && !mappedBy.isEmpty()) {
int i = persister.getEntityMetamodel().getPropertyIndex(mappedBy);
Serializable oldId = null;
if (oldState != null) {
// in case of updating an entity we perhaps have to decache 2 entity collections, this is the
// old one
oldId = getIdentifier(session, oldState[i]);
}
Object ref = persister.getPropertyValue(entity, i);
Serializable id = getIdentifier(session, ref);
// only evict if the related entity has changed
if ((id != null && !id.equals(oldId)) || (oldId != null && !oldId.equals(id))) {
if (id != null) {
evict(id, collectionPersister, session);
}
if (oldId != null) {
evict(oldId, collectionPersister, session);
}
}
} else {
LOG.debug("Evict CollectionRegion " + role);
final SoftLock softLock = collectionPersister.getCacheAccessStrategy().lockRegion();
session.getActionQueue().registerProcess((success, session1) -> {
collectionPersister.getCacheAccessStrategy().unlockRegion(softLock);
});
}
}
} catch (Exception e) {
if (PROPAGATE_EXCEPTION) {
throw new IllegalStateException(e);
}
// don't let decaching influence other logic
LOG.error("", e);
}
}
use of org.hibernate.engine.spi.SessionFactoryImplementor in project hibernate-orm by hibernate.
the class StatefulPersistenceContext method deserialize.
/**
* Used by the owning session to explicitly control deserialization of the persistence context.
*
* @param ois The stream from which the persistence context should be read
* @param session The owning session
*
* @return The deserialized StatefulPersistenceContext
*
* @throws IOException deserialization errors.
* @throws ClassNotFoundException deserialization errors.
*/
public static StatefulPersistenceContext deserialize(ObjectInputStream ois, SessionImplementor session) throws IOException, ClassNotFoundException {
final boolean tracing = LOG.isTraceEnabled();
if (tracing) {
LOG.trace("Serializing persistent-context");
}
final StatefulPersistenceContext rtn = new StatefulPersistenceContext(session);
SessionFactoryImplementor sfi = session.getFactory();
try {
rtn.defaultReadOnly = ois.readBoolean();
// todo : we can actually just determine this from the incoming EntityEntry-s
rtn.hasNonReadOnlyEntities = ois.readBoolean();
int count = ois.readInt();
if (tracing) {
LOG.trace("Starting deserialization of [" + count + "] entitiesByKey entries");
}
rtn.entitiesByKey = new HashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
for (int i = 0; i < count; i++) {
rtn.entitiesByKey.put(EntityKey.deserialize(ois, sfi), ois.readObject());
}
count = ois.readInt();
if (tracing) {
LOG.trace("Starting deserialization of [" + count + "] entitiesByUniqueKey entries");
}
rtn.entitiesByUniqueKey = new HashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
for (int i = 0; i < count; i++) {
rtn.entitiesByUniqueKey.put(EntityUniqueKey.deserialize(ois, session), ois.readObject());
}
count = ois.readInt();
if (tracing) {
LOG.trace("Starting deserialization of [" + count + "] proxiesByKey entries");
}
//noinspection unchecked
rtn.proxiesByKey = new ConcurrentReferenceHashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count, .75f, 1, ConcurrentReferenceHashMap.ReferenceType.STRONG, ConcurrentReferenceHashMap.ReferenceType.WEAK, null);
for (int i = 0; i < count; i++) {
final EntityKey ek = EntityKey.deserialize(ois, sfi);
final Object proxy = ois.readObject();
if (proxy instanceof HibernateProxy) {
((HibernateProxy) proxy).getHibernateLazyInitializer().setSession(session);
rtn.proxiesByKey.put(ek, proxy);
} else {
// otherwise, the proxy was pruned during the serialization process
if (tracing) {
LOG.trace("Encountered pruned proxy");
}
}
}
count = ois.readInt();
if (tracing) {
LOG.trace("Starting deserialization of [" + count + "] entitySnapshotsByKey entries");
}
rtn.entitySnapshotsByKey = new HashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
for (int i = 0; i < count; i++) {
rtn.entitySnapshotsByKey.put(EntityKey.deserialize(ois, sfi), ois.readObject());
}
rtn.entityEntryContext = EntityEntryContext.deserialize(ois, rtn);
count = ois.readInt();
if (tracing) {
LOG.trace("Starting deserialization of [" + count + "] collectionsByKey entries");
}
rtn.collectionsByKey = new HashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
for (int i = 0; i < count; i++) {
rtn.collectionsByKey.put(CollectionKey.deserialize(ois, session), (PersistentCollection) ois.readObject());
}
count = ois.readInt();
if (tracing) {
LOG.trace("Starting deserialization of [" + count + "] collectionEntries entries");
}
rtn.collectionEntries = IdentityMap.instantiateSequenced(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
for (int i = 0; i < count; i++) {
final PersistentCollection pc = (PersistentCollection) ois.readObject();
final CollectionEntry ce = CollectionEntry.deserialize(ois, session);
pc.setCurrentSession(session);
rtn.collectionEntries.put(pc, ce);
}
count = ois.readInt();
if (tracing) {
LOG.trace("Starting deserialization of [" + count + "] arrayHolders entries");
}
rtn.arrayHolders = new IdentityHashMap<>(count < INIT_COLL_SIZE ? INIT_COLL_SIZE : count);
for (int i = 0; i < count; i++) {
rtn.arrayHolders.put(ois.readObject(), (PersistentCollection) ois.readObject());
}
count = ois.readInt();
if (tracing) {
LOG.trace("Starting deserialization of [" + count + "] nullifiableEntityKey entries");
}
rtn.nullifiableEntityKeys = new HashSet<>();
for (int i = 0; i < count; i++) {
rtn.nullifiableEntityKeys.add(EntityKey.deserialize(ois, sfi));
}
} catch (HibernateException he) {
throw new InvalidObjectException(he.getMessage());
}
return rtn;
}
Aggregations