use of org.hibernate.metamodel.spi.MappingMetamodelImplementor 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();
final MappingMetamodelImplementor metamodel = factory.getRuntimeMetamodels().getMappingMetamodel();
Set<String> collectionRoles = metamodel.getCollectionRolesByEntityParticipant(persister.getEntityName());
if (collectionRoles == null || collectionRoles.isEmpty()) {
return;
}
final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
final boolean debugEnabled = LOG.isDebugEnabled();
for (String role : collectionRoles) {
final CollectionPersister collectionPersister = metamodel.getCollectionDescriptor(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 = entityMetamodel.getPropertyIndex(mappedBy);
Object 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.getValue(entity, i);
Object 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 {
if (debugEnabled) {
LOG.debug("Evict CollectionRegion " + role);
}
final CollectionDataAccess cacheAccessStrategy = collectionPersister.getCacheAccessStrategy();
final SoftLock softLock = cacheAccessStrategy.lockRegion();
session.getActionQueue().registerProcess((success, session1) -> cacheAccessStrategy.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.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.
the class DefaultRefreshEventListener method evictCachedCollections.
private void evictCachedCollections(Type[] types, Object id, EventSource source) throws HibernateException {
final ActionQueue actionQueue = source.getActionQueue();
final SessionFactoryImplementor factory = source.getFactory();
final MappingMetamodelImplementor metamodel = factory.getRuntimeMetamodels().getMappingMetamodel();
for (Type type : types) {
if (type.isCollectionType()) {
final String role = ((CollectionType) type).getRole();
CollectionPersister collectionPersister = metamodel.getCollectionDescriptor(role);
if (collectionPersister.hasCache()) {
final CollectionDataAccess cache = collectionPersister.getCacheAccessStrategy();
final Object ck = cache.generateCacheKey(id, collectionPersister, factory, source.getTenantIdentifier());
final SoftLock lock = cache.lockItem(source, ck, null);
cache.remove(source, ck);
actionQueue.registerProcess((success, session) -> cache.unlockItem(session, ck, lock));
}
} else if (type.isComponentType()) {
CompositeType compositeType = (CompositeType) type;
evictCachedCollections(compositeType.getSubtypes(), id, source);
}
}
}
use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.
the class UnionSubclassEntityPersister method generateSubquery.
protected String generateSubquery(Set<String> treated) {
if (!hasSubclasses()) {
return getTableName();
}
final Dialect dialect = getFactory().getJdbcServices().getDialect();
final MappingMetamodelImplementor metamodel = getFactory().getRuntimeMetamodels().getMappingMetamodel();
// Collect all selectables of every entity subtype and group by selection expression as well as table name
final LinkedHashMap<String, Map<String, SelectableMapping>> selectables = new LinkedHashMap<>();
final SelectableConsumer selectableConsumer = (i, selectable) -> {
selectables.computeIfAbsent(selectable.getSelectionExpression(), k -> new HashMap<>()).put(selectable.getContainingTableExpression(), selectable);
};
// Collect the concrete subclass table names for the treated entity names
final Set<String> treatedTableNames = new HashSet<>(treated.size());
for (String subclassName : treated) {
final UnionSubclassEntityPersister subPersister = (UnionSubclassEntityPersister) metamodel.getEntityDescriptor(subclassName);
for (String subclassTableName : subPersister.getSubclassTableNames()) {
if (ArrayHelper.indexOf(subclassSpaces, subclassTableName) != -1) {
treatedTableNames.add(subclassTableName);
}
}
subPersister.getIdentifierMapping().forEachSelectable(selectableConsumer);
if (subPersister.getVersionMapping() != null) {
subPersister.getVersionMapping().forEachSelectable(selectableConsumer);
}
subPersister.visitSubTypeAttributeMappings(attributeMapping -> attributeMapping.forEachSelectable(selectableConsumer));
}
// Create a union sub-query for the table names, like generateSubquery(PersistentClass model, Mapping mapping)
final StringBuilder buf = new StringBuilder(subquery.length()).append("( ");
for (String name : getSubclassEntityNames()) {
final AbstractEntityPersister persister = (AbstractEntityPersister) metamodel.findEntityDescriptor(name);
final String subclassTableName = persister.getTableName();
if (treatedTableNames.contains(subclassTableName)) {
buf.append("select ");
for (Map<String, SelectableMapping> selectableMappings : selectables.values()) {
SelectableMapping selectableMapping = selectableMappings.get(subclassTableName);
if (selectableMapping == null) {
// If there is no selectable mapping for a table name, we render a null expression
selectableMapping = selectableMappings.values().iterator().next();
final int sqlType = selectableMapping.getJdbcMapping().getJdbcType().getDefaultSqlTypeCode();
buf.append(dialect.getSelectClauseNullString(sqlType)).append(" as ");
}
buf.append(new ColumnReference((String) null, selectableMapping, getFactory()).getExpressionText());
buf.append(", ");
}
buf.append(persister.getDiscriminatorSQLValue()).append(" as clazz_");
buf.append(" from ").append(subclassTableName);
buf.append(" union ");
if (dialect.supportsUnionAll()) {
buf.append("all ");
}
}
}
if (buf.length() > 2) {
// chop the last union (all)
buf.setLength(buf.length() - (dialect.supportsUnionAll() ? 11 : 7));
}
return buf.append(" )").toString();
}
use of org.hibernate.metamodel.spi.MappingMetamodelImplementor in project hibernate-orm by hibernate.
the class Util method resolveResultSetMappingClasses.
public static void resolveResultSetMappingClasses(Class[] resultSetMappingClasses, ResultSetMapping resultSetMapping, Consumer<String> querySpaceConsumer, ResultSetMappingResolutionContext context) {
final MappingMetamodelImplementor mappingMetamodel = context.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final JavaTypeRegistry javaTypeRegistry = mappingMetamodel.getTypeConfiguration().getJavaTypeRegistry();
for (Class<?> resultSetMappingClass : resultSetMappingClasses) {
final EntityPersister entityDescriptor = mappingMetamodel.findEntityDescriptor(resultSetMappingClass);
if (entityDescriptor != null) {
resultSetMapping.addResultBuilder(new EntityDomainResultBuilder(entityDescriptor));
for (String querySpace : entityDescriptor.getSynchronizedQuerySpaces()) {
querySpaceConsumer.accept(querySpace);
}
} else {
final JavaType<?> basicType = javaTypeRegistry.getDescriptor(resultSetMappingClass);
if (basicType != null) {
resultSetMapping.addResultBuilder(new ScalarDomainResultBuilder<>(basicType));
}
}
}
}
Aggregations