use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.
the class EnumeratedSmokeTest method testEnumeratedTypeResolutions.
/**
* I personally have been unable to repeoduce the bug as reported in HHH-10402. This test
* is equivalent to what the reporters say happens, but these tests pass fine.
*/
@Test
@TestForIssue(jiraKey = "HHH-10402")
public void testEnumeratedTypeResolutions() {
final MetadataImplementor mappings = (MetadataImplementor) new MetadataSources(ssr).addAnnotatedClass(EntityWithEnumeratedAttributes.class).buildMetadata();
mappings.validate();
final PersistentClass entityBinding = mappings.getEntityBinding(EntityWithEnumeratedAttributes.class.getName());
validateEnumMapping(entityBinding.getProperty("notAnnotated"), EnumType.ORDINAL);
validateEnumMapping(entityBinding.getProperty("noEnumType"), EnumType.ORDINAL);
validateEnumMapping(entityBinding.getProperty("ordinalEnumType"), EnumType.ORDINAL);
validateEnumMapping(entityBinding.getProperty("stringEnumType"), EnumType.STRING);
}
use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.
the class ComponentTest method afterMetadataBuilt.
@Override
protected void afterMetadataBuilt(Metadata metadata) {
// Oracle and Postgres do not have year() functions, so we need to
// redefine the 'User.person.yob' formula
//
// consider temporary until we add the capability to define
// mapping formulas which can use dialect-registered functions...
PersistentClass user = metadata.getEntityBinding(User.class.getName());
org.hibernate.mapping.Property personProperty = user.getProperty("person");
Component component = (Component) personProperty.getValue();
Formula f = (Formula) component.getProperty("yob").getValue().getColumnIterator().next();
SQLFunction yearFunction = metadata.getDatabase().getJdbcEnvironment().getDialect().getFunctions().get("year");
if (yearFunction == null) {
// the dialect not know to support a year() function, so rely on the
// ANSI SQL extract function
f.setFormula("extract( year from dob )");
} else {
List args = new ArrayList();
args.add("dob");
f.setFormula(yearFunction.render(StandardBasicTypes.INTEGER, args, null));
}
}
use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.
the class ToOneFkSecondPass method doSecondPass.
public void doSecondPass(java.util.Map persistentClasses) throws MappingException {
if (value instanceof ManyToOne) {
ManyToOne manyToOne = (ManyToOne) value;
PersistentClass ref = (PersistentClass) persistentClasses.get(manyToOne.getReferencedEntityName());
if (ref == null) {
throw new AnnotationException("@OneToOne or @ManyToOne on " + StringHelper.qualify(entityClassName, path) + " references an unknown entity: " + manyToOne.getReferencedEntityName());
}
manyToOne.setPropertyName(path);
BinderHelper.createSyntheticPropertyReference(columns, ref, null, manyToOne, false, buildingContext);
TableBinder.bindFk(ref, null, columns, manyToOne, unique, buildingContext);
/*
* HbmMetadataSourceProcessorImpl does this only when property-ref != null, but IMO, it makes sense event if it is null
*/
if (!manyToOne.isIgnoreNotFound())
manyToOne.createPropertyRefConstraints(persistentClasses);
} else if (value instanceof OneToOne) {
value.createForeignKey();
} else {
throw new AssertionFailure("FkSecondPass for a wrong value type: " + value.getClass().getName());
}
}
use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.
the class ToOneFkSecondPass method isInPrimaryKey.
@Override
public boolean isInPrimaryKey() {
if (entityClassName == null)
return false;
final PersistentClass persistentClass = buildingContext.getMetadataCollector().getEntityBinding(entityClassName);
Property property = persistentClass.getIdentifierProperty();
if (path == null) {
return false;
} else if (property != null) {
// try explicit identifier property
return path.startsWith(property.getName() + ".");
} else {
// embedded property starts their path with 'id.' See PropertyPreloadedData( ) use when idClass != null in AnnotationSourceProcessor
if (path.startsWith("id.")) {
KeyValue valueIdentifier = persistentClass.getIdentifier();
String localPath = path.substring(3);
if (valueIdentifier instanceof Component) {
Iterator it = ((Component) valueIdentifier).getPropertyIterator();
while (it.hasNext()) {
Property idProperty = (Property) it.next();
if (localPath.startsWith(idProperty.getName()))
return true;
}
}
}
}
return false;
}
use of org.hibernate.mapping.PersistentClass in project hibernate-orm by hibernate.
the class CollectionBinder method bindOneToManySecondPass.
protected void bindOneToManySecondPass(Collection collection, Map persistentClasses, Ejb3JoinColumn[] fkJoinColumns, XClass collectionType, boolean cascadeDeleteEnabled, boolean ignoreNotFound, MetadataBuildingContext buildingContext, Map<XClass, InheritanceState> inheritanceStatePerClass) {
final boolean debugEnabled = LOG.isDebugEnabled();
if (debugEnabled) {
LOG.debugf("Binding a OneToMany: %s.%s through a foreign key", propertyHolder.getEntityName(), propertyName);
}
if (buildingContext == null) {
throw new AssertionFailure("CollectionSecondPass for oneToMany should not be called with null mappings");
}
org.hibernate.mapping.OneToMany oneToMany = new org.hibernate.mapping.OneToMany(buildingContext, collection.getOwner());
collection.setElement(oneToMany);
oneToMany.setReferencedEntityName(collectionType.getName());
oneToMany.setIgnoreNotFound(ignoreNotFound);
String assocClass = oneToMany.getReferencedEntityName();
PersistentClass associatedClass = (PersistentClass) persistentClasses.get(assocClass);
if (jpaOrderBy != null) {
final String orderByFragment = buildOrderByClauseFromHql(jpaOrderBy.value(), associatedClass, collection.getRole());
if (StringHelper.isNotEmpty(orderByFragment)) {
collection.setOrderBy(orderByFragment);
}
}
Map<String, Join> joins = buildingContext.getMetadataCollector().getJoins(assocClass);
if (associatedClass == null) {
throw new MappingException(String.format("Association [%s] for entity [%s] references unmapped class [%s]", propertyName, propertyHolder.getClassName(), assocClass));
}
oneToMany.setAssociatedClass(associatedClass);
for (Ejb3JoinColumn column : fkJoinColumns) {
column.setPersistentClass(associatedClass, joins, inheritanceStatePerClass);
column.setJoins(joins);
collection.setCollectionTable(column.getTable());
}
if (debugEnabled) {
LOG.debugf("Mapping collection: %s -> %s", collection.getRole(), collection.getCollectionTable().getName());
}
bindFilters(false);
bindCollectionSecondPass(collection, null, fkJoinColumns, cascadeDeleteEnabled, property, propertyHolder, buildingContext);
if (!collection.isInverse() && !collection.getKey().isNullable()) {
// for non-inverse one-to-many, with a not-null fk, add a backref!
String entityName = oneToMany.getReferencedEntityName();
PersistentClass referenced = buildingContext.getMetadataCollector().getEntityBinding(entityName);
Backref prop = new Backref();
prop.setName('_' + fkJoinColumns[0].getPropertyName() + '_' + fkJoinColumns[0].getLogicalColumnName() + "Backref");
prop.setUpdateable(false);
prop.setSelectable(false);
prop.setCollectionRole(collection.getRole());
prop.setEntityName(collection.getOwner().getEntityName());
prop.setValue(collection.getKey());
referenced.addProperty(prop);
}
}
Aggregations