use of org.hibernate.metamodel.MappingMetamodel in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method withTreatRestriction.
private Expression withTreatRestriction(Expression expression, SqmPath<?> path) {
final SqmPath<?> lhs;
if (path instanceof SqmTreatedPath<?, ?>) {
lhs = path;
} else {
lhs = path.getLhs();
}
if (lhs instanceof SqmTreatedPath<?, ?>) {
final SqmTreatedPath<?, ?> treatedPath = (SqmTreatedPath<?, ?>) lhs;
final Class<?> treatTargetJavaType = treatedPath.getTreatTarget().getJavaType();
final Class<?> originalJavaType = treatedPath.getWrappedPath().getJavaType();
if (treatTargetJavaType.isAssignableFrom(originalJavaType)) {
// Treating a node to a super type can be ignored
return expression;
}
if (!(expression.getExpressionType() instanceof BasicValuedMapping)) {
// A case wrapper for non-basic paths is not possible,
// because a case expression must return a scalar value,
// so we instead add the type restriction predicate as conjunct
final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final EntityPersister entityDescriptor = domainModel.findEntityDescriptor(treatedPath.getTreatTarget().getHibernateEntityName());
conjunctTreatUsages.computeIfAbsent(treatedPath.getWrappedPath(), p -> new HashSet<>(1)).addAll(entityDescriptor.getSubclassEntityNames());
return expression;
}
// Note: If the columns that are accessed are not shared with other entities, we could avoid this wrapping
return createCaseExpression(treatedPath.getWrappedPath(), treatedPath.getTreatTarget(), expression);
}
return expression;
}
use of org.hibernate.metamodel.MappingMetamodel in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method createTreatTypeRestriction.
private Predicate createTreatTypeRestriction(SqmPath<?> lhs, Set<String> subclassEntityNames) {
final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
// Do what visitSelfInterpretingSqmPath does, except for calling preparingReusablePath
// as that would register a type usage for the table group that we don't want here
final DiscriminatorSqmPath discriminatorSqmPath = (DiscriminatorSqmPath) lhs.type();
registerTypeUsage(discriminatorSqmPath);
final Expression typeExpression = discriminatorSqmPath.interpret(this, this, jpaQueryComplianceEnabled);
if (subclassEntityNames.size() == 1) {
return new ComparisonPredicate(typeExpression, ComparisonOperator.EQUAL, new EntityTypeLiteral(domainModel.findEntityDescriptor(subclassEntityNames.iterator().next())));
} else {
final List<Expression> typeLiterals = new ArrayList<>(subclassEntityNames.size());
for (String subclassEntityName : subclassEntityNames) {
typeLiterals.add(new EntityTypeLiteral(domainModel.findEntityDescriptor(subclassEntityName)));
}
return new InListPredicate(typeExpression, typeLiterals);
}
}
use of org.hibernate.metamodel.MappingMetamodel in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method createTreatTypeRestriction.
private Predicate createTreatTypeRestriction(SqmPath<?> lhs, EntityDomainType<?> treatTarget) {
final MappingMetamodel domainModel = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final EntityPersister entityDescriptor = domainModel.findEntityDescriptor(treatTarget.getHibernateEntityName());
final Set<String> subclassEntityNames = entityDescriptor.getSubclassEntityNames();
return createTreatTypeRestriction(lhs, subclassEntityNames);
}
use of org.hibernate.metamodel.MappingMetamodel in project hibernate-orm by hibernate.
the class UUIDBinaryTest method testUsage.
@Test
public void testUsage(SessionFactoryScope scope) {
final MappingMetamodel domainModel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final EntityPersister entityDescriptor = domainModel.findEntityDescriptor(Node.class);
final List<JdbcMapping> identifierJdbcMappings = entityDescriptor.getIdentifierMapping().getJdbcMappings();
assertThat(identifierJdbcMappings, hasSize(1));
final JdbcMapping jdbcMapping = identifierJdbcMappings.get(0);
assertThat(jdbcMapping.getJdbcType().isBinary(), is(true));
final UUIDPair uuidPair = scope.fromTransaction(session -> {
final Node root = new Node("root");
session.save(root);
assertThat(root.id, notNullValue());
final Node child = new Node("child", root);
session.save(child);
assertThat(child.id, notNullValue());
return new UUIDPair(root.id, child.id);
});
scope.inTransaction(session -> {
final Node root = session.get(Node.class, uuidPair.rootId);
assertThat(root, notNullValue());
final Node child = session.get(Node.class, uuidPair.childId);
assertThat(child, notNullValue());
});
scope.inTransaction(session -> {
final Node node = session.createQuery("from Node n join fetch n.parent where n.parent is not null", Node.class).uniqueResult();
assertThat(node, notNullValue());
assertThat(node.parent, notNullValue());
});
}
use of org.hibernate.metamodel.MappingMetamodel in project hibernate-orm by hibernate.
the class PluralAttributeMappingTests method testArrays.
@Test
public void testArrays(SessionFactoryScope scope) {
final MappingMetamodel domainModel = scope.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel();
final EntityMappingType containerEntityDescriptor = domainModel.getEntityDescriptor(EntityOfArrays.class);
assertThat(containerEntityDescriptor.getNumberOfAttributeMappings(), is(2));
final AttributeMapping arrayOfBasics = containerEntityDescriptor.findAttributeMapping("arrayOfBasics");
assertThat(arrayOfBasics, notNullValue());
}
Aggregations