use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.
the class TypeSafeActivator method findPropertyByName.
/**
* Locate the property by path in a recursive way, including IdentifierProperty in the loop if propertyName is
* {@code null}. If propertyName is {@code null} or empty, the IdentifierProperty is returned
*/
private static Property findPropertyByName(PersistentClass associatedClass, String propertyName) {
Property property = null;
Property idProperty = associatedClass.getIdentifierProperty();
String idName = idProperty != null ? idProperty.getName() : null;
try {
if (propertyName == null || propertyName.length() == 0 || propertyName.equals(idName)) {
//default to id
property = idProperty;
} else {
if (propertyName.indexOf(idName + ".") == 0) {
property = idProperty;
propertyName = propertyName.substring(idName.length() + 1);
}
StringTokenizer st = new StringTokenizer(propertyName, ".", false);
while (st.hasMoreElements()) {
String element = (String) st.nextElement();
if (property == null) {
property = associatedClass.getProperty(element);
} else {
if (!property.isComposite()) {
return null;
}
property = ((Component) property.getValue()).getProperty(element);
}
}
}
} catch (MappingException e) {
try {
//if we do not find it try to check the identifier mapper
if (associatedClass.getIdentifierMapper() == null) {
return null;
}
StringTokenizer st = new StringTokenizer(propertyName, ".", false);
while (st.hasMoreElements()) {
String element = (String) st.nextElement();
if (property == null) {
property = associatedClass.getIdentifierMapper().getProperty(element);
} else {
if (!property.isComposite()) {
return null;
}
property = ((Component) property.getValue()).getProperty(element);
}
}
} catch (MappingException ee) {
return null;
}
}
return property;
}
use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.
the class MetadataContext method wrapUp.
@SuppressWarnings({ "unchecked" })
public void wrapUp() {
final boolean traceEnabled = LOG.isTraceEnabled();
if (traceEnabled) {
LOG.trace("Wrapping up metadata context...");
}
boolean staticMetamodelScanEnabled = JpaStaticMetaModelPopulationSetting.determineJpaMetaModelPopulationSetting(sessionFactory.getProperties()) != JpaStaticMetaModelPopulationSetting.DISABLED;
//we need to process types from superclasses to subclasses
for (Object mapping : orderedMappings) {
if (PersistentClass.class.isAssignableFrom(mapping.getClass())) {
@SuppressWarnings("unchecked") final PersistentClass safeMapping = (PersistentClass) mapping;
if (traceEnabled) {
LOG.trace("Starting entity [" + safeMapping.getEntityName() + ']');
}
try {
final EntityTypeImpl<?> jpa2Mapping = entityTypesByPersistentClass.get(safeMapping);
applyIdMetadata(safeMapping, jpa2Mapping);
applyVersionAttribute(safeMapping, jpa2Mapping);
Iterator<Property> properties = safeMapping.getDeclaredPropertyIterator();
while (properties.hasNext()) {
final Property property = properties.next();
if (property.getValue() == safeMapping.getIdentifierMapper()) {
// #buildIdClassAttributes
continue;
}
if (safeMapping.isVersioned() && property == safeMapping.getVersion()) {
// skip the version property, it was already handled previously.
continue;
}
final Attribute attribute = attributeFactory.buildAttribute(jpa2Mapping, property);
if (attribute != null) {
jpa2Mapping.getBuilder().addAttribute(attribute);
}
}
jpa2Mapping.lock();
if (staticMetamodelScanEnabled) {
populateStaticMetamodel(jpa2Mapping);
}
} finally {
if (traceEnabled) {
LOG.trace("Completed entity [" + safeMapping.getEntityName() + ']');
}
}
} else if (MappedSuperclass.class.isAssignableFrom(mapping.getClass())) {
@SuppressWarnings("unchecked") final MappedSuperclass safeMapping = (MappedSuperclass) mapping;
if (traceEnabled) {
LOG.trace("Starting mapped superclass [" + safeMapping.getMappedClass().getName() + ']');
}
try {
final MappedSuperclassTypeImpl<?> jpa2Mapping = mappedSuperclassByMappedSuperclassMapping.get(safeMapping);
applyIdMetadata(safeMapping, jpa2Mapping);
applyVersionAttribute(safeMapping, jpa2Mapping);
Iterator<Property> properties = safeMapping.getDeclaredPropertyIterator();
while (properties.hasNext()) {
final Property property = properties.next();
if (safeMapping.isVersioned() && property == safeMapping.getVersion()) {
// skip the version property, it was already handled previously.
continue;
}
final Attribute attribute = attributeFactory.buildAttribute(jpa2Mapping, property);
if (attribute != null) {
jpa2Mapping.getBuilder().addAttribute(attribute);
}
}
jpa2Mapping.lock();
if (staticMetamodelScanEnabled) {
populateStaticMetamodel(jpa2Mapping);
}
} finally {
if (traceEnabled) {
LOG.trace("Completed mapped superclass [" + safeMapping.getMappedClass().getName() + ']');
}
}
} else {
throw new AssertionFailure("Unexpected mapping type: " + mapping.getClass());
}
}
if (staticMetamodelScanEnabled) {
for (EmbeddableTypeImpl embeddable : embeddables.values()) {
populateStaticMetamodel(embeddable);
}
}
}
use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.
the class NestedEmbeddableMetadataTest method testEnumTypeInterpretation.
@Test
public void testEnumTypeInterpretation() {
final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().enableAutoClose().applySetting(AvailableSettings.HBM2DDL_AUTO, "create-drop").build();
try {
final Metadata metadata = new MetadataSources(serviceRegistry).addAnnotatedClass(Customer.class).buildMetadata();
PersistentClass classMetadata = metadata.getEntityBinding(Customer.class.getName());
Property investmentsProperty = classMetadata.getProperty("investments");
Collection investmentsValue = (Collection) investmentsProperty.getValue();
Component investmentMetadata = (Component) investmentsValue.getElement();
Value descriptionValue = investmentMetadata.getProperty("description").getValue();
assertEquals(1, descriptionValue.getColumnSpan());
Column selectable = (Column) descriptionValue.getColumnIterator().next();
assertEquals(500, selectable.getLength());
Component amountMetadata = (Component) investmentMetadata.getProperty("amount").getValue();
SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty("currency").getValue();
CustomType currencyType = (CustomType) currencyMetadata.getType();
int[] currencySqlTypes = currencyType.sqlTypes(metadata);
assertEquals(1, currencySqlTypes.length);
assertJdbcTypeCode(Types.VARCHAR, currencySqlTypes[0]);
} finally {
StandardServiceRegistryBuilder.destroy(serviceRegistry);
}
}
use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.
the class FieldAccessedNestedEmbeddableMetadataTest method testEnumTypeInterpretation.
@Test
@FailureExpected(jiraKey = "HHH-9089")
public void testEnumTypeInterpretation() {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
try {
final Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(Customer.class).buildMetadata();
PersistentClass classMetadata = metadata.getEntityBinding(Customer.class.getName());
Property investmentsProperty = classMetadata.getProperty("investments");
Collection investmentsValue = (Collection) investmentsProperty.getValue();
Component investmentMetadata = (Component) investmentsValue.getElement();
Value descriptionValue = investmentMetadata.getProperty("description").getValue();
assertEquals(1, descriptionValue.getColumnSpan());
Column selectable = (Column) descriptionValue.getColumnIterator().next();
assertEquals(500, selectable.getLength());
Component amountMetadata = (Component) investmentMetadata.getProperty("amount").getValue();
SimpleValue currencyMetadata = (SimpleValue) amountMetadata.getProperty("currency").getValue();
CustomType currencyType = (CustomType) currencyMetadata.getType();
int[] currencySqlTypes = currencyType.sqlTypes(metadata);
assertEquals(1, currencySqlTypes.length);
assertJdbcTypeCode(Types.VARCHAR, currencySqlTypes[0]);
} finally {
StandardServiceRegistryBuilder.destroy(ssr);
}
}
use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.
the class ComponentNamingStrategyTest method testComponentSafeNamingStrategy.
@Test
@TestForIssue(jiraKey = "HHH-6005")
public void testComponentSafeNamingStrategy() {
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
try {
final MetadataSources ms = new MetadataSources(ssr);
ms.addAnnotatedClass(Container.class).addAnnotatedClass(Item.class);
final Metadata metadata = ms.getMetadataBuilder().applyImplicitNamingStrategy(ImplicitNamingStrategyComponentPathImpl.INSTANCE).build();
final PersistentClass pc = metadata.getEntityBinding(Container.class.getName());
Property p = pc.getProperty("items");
Bag value = assertTyping(Bag.class, p.getValue());
SimpleValue elementValue = assertTyping(SimpleValue.class, value.getElement());
assertEquals(1, elementValue.getColumnSpan());
Column column = assertTyping(Column.class, elementValue.getColumnIterator().next());
assertEquals("items_name", column.getName());
} finally {
StandardServiceRegistryBuilder.destroy(ssr);
}
}
Aggregations