Search in sources :

Example 76 with PersistentClass

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);
}
Also used : MetadataSources(org.hibernate.boot.MetadataSources) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 77 with PersistentClass

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));
    }
}
Also used : Formula(org.hibernate.mapping.Formula) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SQLFunction(org.hibernate.dialect.function.SQLFunction) Component(org.hibernate.mapping.Component) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 78 with PersistentClass

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());
    }
}
Also used : OneToOne(org.hibernate.mapping.OneToOne) AssertionFailure(org.hibernate.AssertionFailure) AnnotationException(org.hibernate.AnnotationException) ManyToOne(org.hibernate.mapping.ManyToOne) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 79 with PersistentClass

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;
}
Also used : KeyValue(org.hibernate.mapping.KeyValue) Iterator(java.util.Iterator) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 80 with PersistentClass

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);
    }
}
Also used : AssertionFailure(org.hibernate.annotations.common.AssertionFailure) Join(org.hibernate.mapping.Join) Backref(org.hibernate.mapping.Backref) OneToMany(javax.persistence.OneToMany) MappingException(org.hibernate.MappingException) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

PersistentClass (org.hibernate.mapping.PersistentClass)229 Test (org.junit.Test)121 Property (org.hibernate.mapping.Property)65 MetadataSources (org.hibernate.boot.MetadataSources)53 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)52 RootClass (org.hibernate.mapping.RootClass)50 Metadata (org.hibernate.boot.Metadata)45 Column (org.hibernate.mapping.Column)43 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)42 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)38 Iterator (java.util.Iterator)30 SimpleValue (org.hibernate.mapping.SimpleValue)24 HashMap (java.util.HashMap)22 Collection (org.hibernate.mapping.Collection)21 MappingException (org.hibernate.MappingException)20 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)20 Table (org.hibernate.mapping.Table)20 Map (java.util.Map)19 ArrayList (java.util.ArrayList)18 TestForIssue (org.hibernate.testing.TestForIssue)18