use of org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy in project hibernate-orm by hibernate.
the class NaturalIdXrefDelegate method removeNaturalIdCrossReference.
/**
* Handle removing cross reference entries for the given natural-id/pk combo
*
* @param persister The persister representing the entity type.
* @param pk The primary key value
* @param naturalIdValues The natural id value(s)
*
* @return The cached values, if any. May be different from incoming values.
*/
public Object[] removeNaturalIdCrossReference(EntityPersister persister, Serializable pk, Object[] naturalIdValues) {
persister = locatePersisterForKey(persister);
validateNaturalId(persister, naturalIdValues);
final NaturalIdResolutionCache entityNaturalIdResolutionCache = naturalIdResolutionCacheMap.get(persister);
Object[] sessionCachedNaturalIdValues = null;
if (entityNaturalIdResolutionCache != null) {
final CachedNaturalId cachedNaturalId = entityNaturalIdResolutionCache.pkToNaturalIdMap.remove(pk);
if (cachedNaturalId != null) {
entityNaturalIdResolutionCache.naturalIdToPkMap.remove(cachedNaturalId);
sessionCachedNaturalIdValues = cachedNaturalId.getValues();
}
}
if (persister.hasNaturalIdCache()) {
final NaturalIdRegionAccessStrategy naturalIdCacheAccessStrategy = persister.getNaturalIdCacheAccessStrategy();
final Object naturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey(naturalIdValues, persister, session());
naturalIdCacheAccessStrategy.evict(naturalIdCacheKey);
if (sessionCachedNaturalIdValues != null && !Arrays.equals(sessionCachedNaturalIdValues, naturalIdValues)) {
final Object sessionNaturalIdCacheKey = naturalIdCacheAccessStrategy.generateCacheKey(sessionCachedNaturalIdValues, persister, session());
naturalIdCacheAccessStrategy.evict(sessionNaturalIdCacheKey);
}
}
return sessionCachedNaturalIdValues;
}
use of org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy in project hibernate-orm by hibernate.
the class DefaultResolveNaturalIdEventListener method loadFromDatasource.
/**
* Performs the process of loading an entity from the configured
* underlying datasource.
*
* @param event The load event
*
* @return The object loaded from the datasource, or null if not found.
*/
protected Serializable loadFromDatasource(final ResolveNaturalIdEvent event) {
final SessionFactoryImplementor factory = event.getSession().getFactory();
final boolean stats = factory.getStatistics().isStatisticsEnabled();
long startTime = 0;
if (stats) {
startTime = System.nanoTime();
}
final Serializable pk = event.getEntityPersister().loadEntityIdByNaturalId(event.getOrderedNaturalIdValues(), event.getLockOptions(), event.getSession());
if (stats) {
final NaturalIdRegionAccessStrategy naturalIdCacheAccessStrategy = event.getEntityPersister().getNaturalIdCacheAccessStrategy();
final String regionName = naturalIdCacheAccessStrategy == null ? null : naturalIdCacheAccessStrategy.getRegion().getName();
final long endTime = System.nanoTime();
final long milliseconds = TimeUnit.MILLISECONDS.convert(endTime - startTime, TimeUnit.NANOSECONDS);
factory.getStatisticsImplementor().naturalIdQueryExecuted(regionName, milliseconds);
}
//PK can be null if the entity doesn't exist
if (pk != null) {
event.getSession().getPersistenceContext().getNaturalIdHelper().cacheNaturalIdCrossReferenceFromLoad(event.getEntityPersister(), pk, event.getOrderedNaturalIdValues());
}
return pk;
}
use of org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy in project hibernate-orm by hibernate.
the class CacheImpl method determineNaturalIdRegionAccessStrategy.
@Override
public NaturalIdRegionAccessStrategy determineNaturalIdRegionAccessStrategy(PersistentClass model) {
NaturalIdRegionAccessStrategy naturalIdAccessStrategy = null;
if (model.hasNaturalId() && model.getNaturalIdCacheRegionName() != null) {
final String naturalIdCacheRegionName = cacheRegionPrefix + model.getNaturalIdCacheRegionName();
naturalIdAccessStrategy = naturalIdRegionAccessStrategyMap.get(naturalIdCacheRegionName);
if (naturalIdAccessStrategy == null && settings.isSecondLevelCacheEnabled()) {
final CacheDataDescriptionImpl cacheDataDescription = CacheDataDescriptionImpl.decode(model);
NaturalIdRegion naturalIdRegion = null;
try {
naturalIdRegion = regionFactory.buildNaturalIdRegion(naturalIdCacheRegionName, sessionFactory.getProperties(), cacheDataDescription);
} catch (UnsupportedOperationException e) {
LOG.warnf("Shared cache region factory [%s] does not support natural id caching; " + "shared NaturalId caching will be disabled for not be enabled for %s", regionFactory.getClass().getName(), model.getEntityName());
}
if (naturalIdRegion != null) {
naturalIdAccessStrategy = naturalIdRegion.buildAccessStrategy(regionFactory.getDefaultAccessType());
naturalIdRegionAccessStrategyMap.put(naturalIdCacheRegionName, naturalIdAccessStrategy);
}
}
}
return naturalIdAccessStrategy;
}
use of org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy in project hibernate-orm by hibernate.
the class ConcurrentStatisticsImpl method getNaturalIdCacheStatistics.
@Override
public ConcurrentNaturalIdCacheStatisticsImpl getNaturalIdCacheStatistics(String regionName) {
ConcurrentNaturalIdCacheStatisticsImpl stat = naturalIdCacheStatistics.get(regionName);
if (stat == null) {
if (sessionFactory == null) {
return null;
}
final NaturalIdRegionAccessStrategy accessStrategy = sessionFactory.getCache().getNaturalIdCacheRegionAccessStrategy(regionName);
stat = new ConcurrentNaturalIdCacheStatisticsImpl(accessStrategy.getRegion(), accessStrategy);
ConcurrentNaturalIdCacheStatisticsImpl previous;
if ((previous = naturalIdCacheStatistics.putIfAbsent(regionName, stat)) != null) {
stat = previous;
}
}
return stat;
}
use of org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy in project hibernate-orm by hibernate.
the class MetamodelImpl method initialize.
/**
* Prepare the metamodel using the information from the collection of Hibernate
* {@link PersistentClass} models
*
* @param mappingMetadata The mapping information
* @param jpaMetaModelPopulationSetting Should the JPA Metamodel be built as well?
*/
public void initialize(MetadataImplementor mappingMetadata, JpaMetaModelPopulationSetting jpaMetaModelPopulationSetting) {
this.imports.putAll(mappingMetadata.getImports());
final PersisterCreationContext persisterCreationContext = new PersisterCreationContext() {
@Override
public SessionFactoryImplementor getSessionFactory() {
return sessionFactory;
}
@Override
public MetadataImplementor getMetadata() {
return mappingMetadata;
}
};
final PersisterFactory persisterFactory = sessionFactory.getServiceRegistry().getService(PersisterFactory.class);
for (final PersistentClass model : mappingMetadata.getEntityBindings()) {
final EntityRegionAccessStrategy accessStrategy = sessionFactory.getCache().determineEntityRegionAccessStrategy(model);
final NaturalIdRegionAccessStrategy naturalIdAccessStrategy = sessionFactory.getCache().determineNaturalIdRegionAccessStrategy(model);
final EntityPersister cp = persisterFactory.createEntityPersister(model, accessStrategy, naturalIdAccessStrategy, persisterCreationContext);
entityPersisterMap.put(model.getEntityName(), cp);
if (cp.getConcreteProxyClass() != null && cp.getConcreteProxyClass().isInterface() && !Map.class.isAssignableFrom(cp.getConcreteProxyClass()) && cp.getMappedClass() != cp.getConcreteProxyClass()) {
if (cp.getMappedClass().equals(cp.getConcreteProxyClass())) {
// this part handles an odd case in the Hibernate test suite where we map an interface
// as the class and the proxy. I cannot think of a real life use case for that
// specific test, but..
log.debugf("Entity [%s] mapped same interface [%s] as class and proxy", cp.getEntityName(), cp.getMappedClass());
} else {
final String old = entityProxyInterfaceMap.put(cp.getConcreteProxyClass(), cp.getEntityName());
if (old != null) {
throw new HibernateException(String.format(Locale.ENGLISH, "Multiple entities [%s, %s] named the same interface [%s] as their proxy which is not supported", old, cp.getEntityName(), cp.getConcreteProxyClass().getName()));
}
}
}
}
for (final Collection model : mappingMetadata.getCollectionBindings()) {
final CollectionRegionAccessStrategy accessStrategy = sessionFactory.getCache().determineCollectionRegionAccessStrategy(model);
final CollectionPersister persister = persisterFactory.createCollectionPersister(model, accessStrategy, persisterCreationContext);
collectionPersisterMap.put(model.getRole(), persister);
Type indexType = persister.getIndexType();
if (indexType != null && indexType.isAssociationType() && !indexType.isAnyType()) {
String entityName = ((AssociationType) indexType).getAssociatedEntityName(sessionFactory);
Set<String> roles = collectionRolesByEntityParticipant.get(entityName);
if (roles == null) {
roles = new HashSet<>();
collectionRolesByEntityParticipant.put(entityName, roles);
}
roles.add(persister.getRole());
}
Type elementType = persister.getElementType();
if (elementType.isAssociationType() && !elementType.isAnyType()) {
String entityName = ((AssociationType) elementType).getAssociatedEntityName(sessionFactory);
Set<String> roles = collectionRolesByEntityParticipant.get(entityName);
if (roles == null) {
roles = new HashSet<>();
collectionRolesByEntityParticipant.put(entityName, roles);
}
roles.add(persister.getRole());
}
}
// after *all* persisters and named queries are registered
entityPersisterMap.values().forEach(EntityPersister::generateEntityDefinition);
for (EntityPersister persister : entityPersisterMap.values()) {
persister.postInstantiate();
registerEntityNameResolvers(persister, entityNameResolvers);
}
collectionPersisterMap.values().forEach(CollectionPersister::postInstantiate);
if (jpaMetaModelPopulationSetting != JpaMetaModelPopulationSetting.DISABLED) {
MetadataContext context = new MetadataContext(sessionFactory, mappingMetadata.getMappedSuperclassMappingsCopy(), jpaMetaModelPopulationSetting);
for (PersistentClass entityBinding : mappingMetadata.getEntityBindings()) {
locateOrBuildEntityType(entityBinding, context);
}
handleUnusedMappedSuperclasses(context);
context.wrapUp();
this.jpaEntityTypeMap.putAll(context.getEntityTypeMap());
this.jpaEmbeddableTypeMap.putAll(context.getEmbeddableTypeMap());
this.jpaMappedSuperclassTypeMap.putAll(context.getMappedSuperclassTypeMap());
this.jpaEntityTypesByEntityName.putAll(context.getEntityTypesByEntityName());
applyNamedEntityGraphs(mappingMetadata.getNamedEntityGraphs().values());
}
}
Aggregations