use of org.hibernate.metamodel.RuntimeMetamodels in project hibernate-orm by hibernate.
the class AbstractCommonQueryContract method applyGraph.
protected void applyGraph(String graphString, GraphSemantic graphSemantic) {
final int separatorPosition = graphString.indexOf('(');
final int terminatorPosition = graphString.lastIndexOf(')');
if (separatorPosition < 0 || terminatorPosition < 0) {
throw new IllegalArgumentException(String.format(ROOT, "Invalid entity-graph definition `%s`; expected form `${EntityName}( ${property1} ... )", graphString));
}
final RuntimeMetamodels runtimeMetamodels = getSession().getFactory().getRuntimeMetamodels();
final JpaMetamodel jpaMetamodel = runtimeMetamodels.getJpaMetamodel();
final String entityName = runtimeMetamodels.getImportedName(graphString.substring(0, separatorPosition).trim());
final String graphNodes = graphString.substring(separatorPosition + 1, terminatorPosition);
final RootGraphImpl<?> rootGraph = new RootGraphImpl<>(null, jpaMetamodel.entity(entityName), jpaMetamodel);
GraphParser.parseInto((EntityGraph<?>) rootGraph, graphNodes, getSession().getSessionFactory());
applyGraph(rootGraph, graphSemantic);
}
use of org.hibernate.metamodel.RuntimeMetamodels in project hibernate-orm by hibernate.
the class Builders method entityCalculated.
/**
* Creates a EntityResultBuilder that does not allow any further configuring of the mapping.
*
* @see #entityCalculated(String, String, SessionFactoryImplementor)
* @see NativeQuery#addEntity(String, Class, LockMode)
* @see NativeQuery#addEntity(String, String, LockMode)
*/
public static DynamicResultBuilderEntityCalculated entityCalculated(String tableAlias, String entityName, LockMode explicitLockMode, SessionFactoryImplementor sessionFactory) {
final RuntimeMetamodels runtimeMetamodels = sessionFactory.getRuntimeMetamodels();
final EntityMappingType entityMapping = runtimeMetamodels.getEntityMappingType(entityName);
return new DynamicResultBuilderEntityCalculated(entityMapping, tableAlias, explicitLockMode, sessionFactory);
}
use of org.hibernate.metamodel.RuntimeMetamodels in project hibernate-orm by hibernate.
the class Builders method entity.
/**
* Creates a EntityResultBuilder allowing for further configuring of the mapping.
*/
public static DynamicResultBuilderEntityStandard entity(String tableAlias, String entityName, SessionFactoryImplementor sessionFactory) {
final RuntimeMetamodels runtimeMetamodels = sessionFactory.getRuntimeMetamodels();
final EntityMappingType entityMapping = runtimeMetamodels.getEntityMappingType(entityName);
return new DynamicResultBuilderEntityStandard(entityMapping, tableAlias);
}
use of org.hibernate.metamodel.RuntimeMetamodels in project hibernate-orm by hibernate.
the class ImmutableManyToOneNaturalIdHbmTest method checkingMapping.
@Test
@TestForIssue(jiraKey = "HHH-10360")
public void checkingMapping(SessionFactoryScope scope) {
final RuntimeMetamodels runtimeMetamodels = scope.getSessionFactory().getRuntimeMetamodels();
final EntityMappingType childMapping = runtimeMetamodels.getEntityMappingType(Child.class.getName());
final EntityPersister persister = childMapping.getEntityPersister();
final EntityMetamodel entityMetamodel = persister.getEntityMetamodel();
final int nameIndex = entityMetamodel.getPropertyIndex("name");
final int parentIndex = entityMetamodel.getPropertyIndex("parent");
// checking alphabetic sort in relation to EntityPersister/EntityMetamodel
assertThat(nameIndex, lessThan(parentIndex));
assertFalse(persister.getPropertyUpdateability()[nameIndex]);
assertFalse(persister.getPropertyUpdateability()[parentIndex]);
// nullability is not specified for either properties making up
// the natural ID, so they should be non-nullable by hbm-specific default
assertFalse(persister.getPropertyNullability()[nameIndex]);
assertFalse(persister.getPropertyNullability()[parentIndex]);
final NaturalIdMapping naturalIdMapping = childMapping.getNaturalIdMapping();
assertNotNull(naturalIdMapping);
assertThat(naturalIdMapping.getNaturalIdAttributes().size(), is(2));
final SingularAttributeMapping first = naturalIdMapping.getNaturalIdAttributes().get(0);
assertThat(first.getAttributeName(), is("name"));
final AttributeMetadata firstMetadata = first.getAttributeMetadataAccess().resolveAttributeMetadata(null);
assertFalse(firstMetadata.getMutabilityPlan().isMutable());
final SingularAttributeMapping second = naturalIdMapping.getNaturalIdAttributes().get(1);
assertThat(second.getAttributeName(), is("parent"));
final AttributeMetadata secondMetadata = second.getAttributeMetadataAccess().resolveAttributeMetadata(null);
assertFalse(secondMetadata.getMutabilityPlan().isMutable());
}
use of org.hibernate.metamodel.RuntimeMetamodels in project hibernate-orm by hibernate.
the class EmbeddableAccessTests method verifyRuntimeModel.
@Test
public void verifyRuntimeModel(SessionFactoryScope scope) {
final RuntimeMetamodels runtimeMetamodels = scope.getSessionFactory().getRuntimeMetamodels();
final EntityMappingType personDescriptor = runtimeMetamodels.getEntityMappingType(Person.class);
// Person defines FIELD access, while Name uses PROPERTY
// - if we find the property annotations, the attribute names will be
// `firstName` and `lastName`, and the columns `first_name` and `last_name`
// - otherwise, we have property and column names being `first` and `last`
final EmbeddableMappingType nameEmbeddable = ((EmbeddedAttributeMapping) personDescriptor.findAttributeMapping("name")).getEmbeddableTypeDescriptor();
assertThat(nameEmbeddable.getNumberOfAttributeMappings()).isEqualTo(2);
final AttributeMapping nameFirst = nameEmbeddable.getAttributeMapping(0);
final AttributeMapping nameLast = nameEmbeddable.getAttributeMapping(1);
assertThat(nameFirst.getAttributeName()).isEqualTo("firstName");
assertThat(nameLast.getAttributeName()).isEqualTo("lastName");
final PluralAttributeMapping aliasesAttribute = (PluralAttributeMapping) personDescriptor.findAttributeMapping("aliases");
final EmbeddableMappingType aliasEmbeddable = ((EmbeddedCollectionPart) aliasesAttribute.getElementDescriptor()).getEmbeddableTypeDescriptor();
assertThat(aliasEmbeddable.getNumberOfAttributeMappings()).isEqualTo(2);
final AttributeMapping aliasFirst = nameEmbeddable.getAttributeMapping(0);
final AttributeMapping aliasLast = nameEmbeddable.getAttributeMapping(1);
assertThat(aliasFirst.getAttributeName()).isEqualTo("firstName");
assertThat(aliasLast.getAttributeName()).isEqualTo("lastName");
}
Aggregations