Search in sources :

Example 1 with PrimaryKeyJoinColumn

use of javax.persistence.PrimaryKeyJoinColumn in project hibernate-orm by hibernate.

the class AnnotationBinder method bindClass.

/**
	 * Bind a class having JSR175 annotations. Subclasses <b>have to</b> be bound afterQuery its parent class.
	 *
	 * @param clazzToProcess entity to bind as {@code XClass} instance
	 * @param inheritanceStatePerClass Meta data about the inheritance relationships for all mapped classes
	 *
	 * @throws MappingException in case there is an configuration error
	 */
public static void bindClass(XClass clazzToProcess, Map<XClass, InheritanceState> inheritanceStatePerClass, MetadataBuildingContext context) throws MappingException {
    //@Entity and @MappedSuperclass on the same class leads to a NPE down the road
    if (clazzToProcess.isAnnotationPresent(Entity.class) && clazzToProcess.isAnnotationPresent(MappedSuperclass.class)) {
        throw new AnnotationException("An entity cannot be annotated with both @Entity and @MappedSuperclass: " + clazzToProcess.getName());
    }
    //TODO: be more strict with secondarytable allowance (not for ids, not for secondary table join columns etc)
    InheritanceState inheritanceState = inheritanceStatePerClass.get(clazzToProcess);
    AnnotatedClassType classType = context.getMetadataCollector().getClassType(clazzToProcess);
    //Queries declared in MappedSuperclass should be usable in Subclasses
    if (AnnotatedClassType.EMBEDDABLE_SUPERCLASS.equals(classType)) {
        bindQueries(clazzToProcess, context);
        bindTypeDefs(clazzToProcess, context);
        bindFilterDefs(clazzToProcess, context);
    }
    if (!isEntityClassType(clazzToProcess, classType)) {
        return;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debugf("Binding entity from annotated class: %s", clazzToProcess.getName());
    }
    PersistentClass superEntity = getSuperEntity(clazzToProcess, inheritanceStatePerClass, context, inheritanceState);
    PersistentClass persistentClass = makePersistentClass(inheritanceState, superEntity, context);
    Entity entityAnn = clazzToProcess.getAnnotation(Entity.class);
    org.hibernate.annotations.Entity hibEntityAnn = clazzToProcess.getAnnotation(org.hibernate.annotations.Entity.class);
    EntityBinder entityBinder = new EntityBinder(entityAnn, hibEntityAnn, clazzToProcess, persistentClass, context);
    entityBinder.setInheritanceState(inheritanceState);
    bindQueries(clazzToProcess, context);
    bindFilterDefs(clazzToProcess, context);
    bindTypeDefs(clazzToProcess, context);
    bindFetchProfiles(clazzToProcess, context);
    BinderHelper.bindAnyMetaDefs(clazzToProcess, context);
    String schema = "";
    //might be no @Table annotation on the annotated class
    String table = "";
    String catalog = "";
    List<UniqueConstraintHolder> uniqueConstraints = new ArrayList<UniqueConstraintHolder>();
    javax.persistence.Table tabAnn = null;
    if (clazzToProcess.isAnnotationPresent(javax.persistence.Table.class)) {
        tabAnn = clazzToProcess.getAnnotation(javax.persistence.Table.class);
        table = tabAnn.name();
        schema = tabAnn.schema();
        catalog = tabAnn.catalog();
        uniqueConstraints = TableBinder.buildUniqueConstraintHolders(tabAnn.uniqueConstraints());
    }
    Ejb3JoinColumn[] inheritanceJoinedColumns = makeInheritanceJoinColumns(clazzToProcess, context, inheritanceState, superEntity);
    final Ejb3DiscriminatorColumn discriminatorColumn;
    if (InheritanceType.SINGLE_TABLE.equals(inheritanceState.getType())) {
        discriminatorColumn = processSingleTableDiscriminatorProperties(clazzToProcess, context, inheritanceState, entityBinder);
    } else if (InheritanceType.JOINED.equals(inheritanceState.getType())) {
        discriminatorColumn = processJoinedDiscriminatorProperties(clazzToProcess, context, inheritanceState, entityBinder);
    } else {
        discriminatorColumn = null;
    }
    entityBinder.setProxy(clazzToProcess.getAnnotation(Proxy.class));
    entityBinder.setBatchSize(clazzToProcess.getAnnotation(BatchSize.class));
    entityBinder.setWhere(clazzToProcess.getAnnotation(Where.class));
    entityBinder.setCache(determineCacheSettings(clazzToProcess, context));
    entityBinder.setNaturalIdCache(clazzToProcess, clazzToProcess.getAnnotation(NaturalIdCache.class));
    bindFilters(clazzToProcess, entityBinder, context);
    entityBinder.bindEntity();
    if (inheritanceState.hasTable()) {
        Check checkAnn = clazzToProcess.getAnnotation(Check.class);
        String constraints = checkAnn == null ? null : checkAnn.constraints();
        EntityTableXref denormalizedTableXref = inheritanceState.hasDenormalizedTable() ? context.getMetadataCollector().getEntityTableXref(superEntity.getEntityName()) : null;
        entityBinder.bindTable(schema, catalog, table, uniqueConstraints, constraints, denormalizedTableXref);
    } else {
        if (clazzToProcess.isAnnotationPresent(Table.class)) {
            LOG.invalidTableAnnotation(clazzToProcess.getName());
        }
        if (inheritanceState.getType() == InheritanceType.SINGLE_TABLE) {
            // we at least need to properly set up the EntityTableXref
            entityBinder.bindTableForDiscriminatedSubclass(context.getMetadataCollector().getEntityTableXref(superEntity.getEntityName()));
        }
    }
    PropertyHolder propertyHolder = PropertyHolderBuilder.buildPropertyHolder(clazzToProcess, persistentClass, entityBinder, context, inheritanceStatePerClass);
    javax.persistence.SecondaryTable secTabAnn = clazzToProcess.getAnnotation(javax.persistence.SecondaryTable.class);
    javax.persistence.SecondaryTables secTabsAnn = clazzToProcess.getAnnotation(javax.persistence.SecondaryTables.class);
    entityBinder.firstLevelSecondaryTablesBinding(secTabAnn, secTabsAnn);
    OnDelete onDeleteAnn = clazzToProcess.getAnnotation(OnDelete.class);
    boolean onDeleteAppropriate = false;
    // todo : sucks that this is separate from RootClass distinction
    final boolean isInheritanceRoot = !inheritanceState.hasParents();
    final boolean hasSubclasses = inheritanceState.hasSiblings();
    if (InheritanceType.JOINED.equals(inheritanceState.getType())) {
        if (inheritanceState.hasParents()) {
            onDeleteAppropriate = true;
            final JoinedSubclass jsc = (JoinedSubclass) persistentClass;
            SimpleValue key = new DependantValue(context.getMetadataCollector(), jsc.getTable(), jsc.getIdentifier());
            jsc.setKey(key);
            ForeignKey fk = clazzToProcess.getAnnotation(ForeignKey.class);
            if (fk != null && !BinderHelper.isEmptyAnnotationValue(fk.name())) {
                key.setForeignKeyName(fk.name());
            } else {
                final PrimaryKeyJoinColumn pkJoinColumn = clazzToProcess.getAnnotation(PrimaryKeyJoinColumn.class);
                final PrimaryKeyJoinColumns pkJoinColumns = clazzToProcess.getAnnotation(PrimaryKeyJoinColumns.class);
                if (pkJoinColumns != null && pkJoinColumns.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) {
                    // don't apply a constraint based on ConstraintMode
                    key.setForeignKeyName("none");
                } else if (pkJoinColumns != null && !StringHelper.isEmpty(pkJoinColumns.foreignKey().name())) {
                    key.setForeignKeyName(pkJoinColumns.foreignKey().name());
                } else if (pkJoinColumn != null && pkJoinColumn.foreignKey().value() == ConstraintMode.NO_CONSTRAINT) {
                    // don't apply a constraint based on ConstraintMode
                    key.setForeignKeyName("none");
                } else if (pkJoinColumn != null && !StringHelper.isEmpty(pkJoinColumn.foreignKey().name())) {
                    key.setForeignKeyName(pkJoinColumn.foreignKey().name());
                }
            }
            if (onDeleteAnn != null) {
                key.setCascadeDeleteEnabled(OnDeleteAction.CASCADE.equals(onDeleteAnn.action()));
            } else {
                key.setCascadeDeleteEnabled(false);
            }
            //we are never in a second pass at that stage, so queue it
            context.getMetadataCollector().addSecondPass(new JoinedSubclassFkSecondPass(jsc, inheritanceJoinedColumns, key, context));
            context.getMetadataCollector().addSecondPass(new CreateKeySecondPass(jsc));
        }
        if (isInheritanceRoot) {
            // (it is perfectly valid for joined subclasses to not have discriminators).
            if (discriminatorColumn != null) {
                // we have a discriminator column
                if (hasSubclasses || !discriminatorColumn.isImplicit()) {
                    bindDiscriminatorColumnToRootPersistentClass((RootClass) persistentClass, discriminatorColumn, entityBinder.getSecondaryTables(), propertyHolder, context);
                    //bind it again since the type might have changed
                    entityBinder.bindDiscriminatorValue();
                }
            }
        }
    } else if (InheritanceType.SINGLE_TABLE.equals(inheritanceState.getType())) {
        if (isInheritanceRoot) {
            if (hasSubclasses || !discriminatorColumn.isImplicit()) {
                bindDiscriminatorColumnToRootPersistentClass((RootClass) persistentClass, discriminatorColumn, entityBinder.getSecondaryTables(), propertyHolder, context);
                //bind it again since the type might have changed
                entityBinder.bindDiscriminatorValue();
            }
        }
    }
    if (onDeleteAnn != null && !onDeleteAppropriate) {
        LOG.invalidOnDeleteAnnotation(propertyHolder.getEntityName());
    }
    // try to find class level generators
    HashMap<String, IdentifierGeneratorDefinition> classGenerators = buildLocalGenerators(clazzToProcess, context);
    // check properties
    final InheritanceState.ElementsToProcess elementsToProcess = inheritanceState.getElementsToProcess();
    inheritanceState.postProcess(persistentClass, entityBinder);
    final boolean subclassAndSingleTableStrategy = inheritanceState.getType() == InheritanceType.SINGLE_TABLE && inheritanceState.hasParents();
    Set<String> idPropertiesIfIdClass = new HashSet<String>();
    boolean isIdClass = mapAsIdClass(inheritanceStatePerClass, inheritanceState, persistentClass, entityBinder, propertyHolder, elementsToProcess, idPropertiesIfIdClass, context);
    if (!isIdClass) {
        entityBinder.setWrapIdsInEmbeddedComponents(elementsToProcess.getIdPropertyCount() > 1);
    }
    processIdPropertiesIfNotAlready(inheritanceStatePerClass, context, persistentClass, entityBinder, propertyHolder, classGenerators, elementsToProcess, subclassAndSingleTableStrategy, idPropertiesIfIdClass);
    if (!inheritanceState.hasParents()) {
        final RootClass rootClass = (RootClass) persistentClass;
        context.getMetadataCollector().addSecondPass(new CreateKeySecondPass(rootClass));
    } else {
        superEntity.addSubclass((Subclass) persistentClass);
    }
    context.getMetadataCollector().addEntityBinding(persistentClass);
    //Process secondary tables and complementary definitions (ie o.h.a.Table)
    context.getMetadataCollector().addSecondPass(new SecondaryTableSecondPass(entityBinder, propertyHolder, clazzToProcess));
    //add process complementary Table definition (index & all)
    entityBinder.processComplementaryTableDefinitions(clazzToProcess.getAnnotation(org.hibernate.annotations.Table.class));
    entityBinder.processComplementaryTableDefinitions(clazzToProcess.getAnnotation(org.hibernate.annotations.Tables.class));
    entityBinder.processComplementaryTableDefinitions(tabAnn);
}
Also used : Entity(javax.persistence.Entity) ArrayList(java.util.ArrayList) Check(org.hibernate.annotations.Check) AnnotationException(org.hibernate.AnnotationException) JoinedSubclass(org.hibernate.mapping.JoinedSubclass) PersistentClass(org.hibernate.mapping.PersistentClass) HashSet(java.util.HashSet) RootClass(org.hibernate.mapping.RootClass) BatchSize(org.hibernate.annotations.BatchSize) DependantValue(org.hibernate.mapping.DependantValue) EntityTableXref(org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref) Where(org.hibernate.annotations.Where) OnDelete(org.hibernate.annotations.OnDelete) Proxy(org.hibernate.annotations.Proxy) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) Table(javax.persistence.Table) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) NaturalIdCache(org.hibernate.annotations.NaturalIdCache) Table(javax.persistence.Table) CollectionTable(javax.persistence.CollectionTable) JoinTable(javax.persistence.JoinTable) ForeignKey(org.hibernate.annotations.ForeignKey) SimpleValue(org.hibernate.mapping.SimpleValue) MappedSuperclass(javax.persistence.MappedSuperclass) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) EntityBinder(org.hibernate.cfg.annotations.EntityBinder)

Example 2 with PrimaryKeyJoinColumn

use of javax.persistence.PrimaryKeyJoinColumn in project hibernate-orm by hibernate.

the class Ejb3JoinColumn method buildJoinColumn.

/**
	 * Build JoinColumn for a JOINED hierarchy
	 */
public static Ejb3JoinColumn buildJoinColumn(PrimaryKeyJoinColumn pkJoinAnn, JoinColumn joinAnn, Value identifier, Map<String, Join> joins, PropertyHolder propertyHolder, MetadataBuildingContext context) {
    final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer();
    Column col = (Column) identifier.getColumnIterator().next();
    String defaultName = context.getMetadataCollector().getLogicalColumnName(identifier.getTable(), col.getQuotedName());
    if (pkJoinAnn != null || joinAnn != null) {
        String colName;
        String columnDefinition;
        String referencedColumnName;
        if (pkJoinAnn != null) {
            colName = pkJoinAnn.name();
            columnDefinition = pkJoinAnn.columnDefinition();
            referencedColumnName = pkJoinAnn.referencedColumnName();
        } else {
            colName = joinAnn.name();
            columnDefinition = joinAnn.columnDefinition();
            referencedColumnName = joinAnn.referencedColumnName();
        }
        final String sqlType;
        if (columnDefinition.equals("")) {
            sqlType = null;
        } else {
            sqlType = normalizer.toDatabaseIdentifierText(columnDefinition);
        }
        final String name;
        if ("".equals(colName)) {
            name = normalizer.normalizeIdentifierQuotingAsString(defaultName);
        } else {
            name = context.getObjectNameNormalizer().normalizeIdentifierQuotingAsString(colName);
        }
        return new Ejb3JoinColumn(sqlType, name, false, false, true, true, referencedColumnName, null, joins, propertyHolder, null, null, false, context);
    } else {
        defaultName = context.getObjectNameNormalizer().normalizeIdentifierQuotingAsString(defaultName);
        return new Ejb3JoinColumn(null, defaultName, false, false, true, true, null, null, joins, propertyHolder, null, null, true, context);
    }
}
Also used : JoinColumn(javax.persistence.JoinColumn) Column(org.hibernate.mapping.Column) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) ObjectNameNormalizer(org.hibernate.boot.model.naming.ObjectNameNormalizer)

Example 3 with PrimaryKeyJoinColumn

use of javax.persistence.PrimaryKeyJoinColumn in project hibernate-orm by hibernate.

the class EntityBinder method createPrimaryColumnsToSecondaryTable.

private void createPrimaryColumnsToSecondaryTable(Object uncastedColumn, PropertyHolder propertyHolder, Join join) {
    Ejb3JoinColumn[] ejb3JoinColumns;
    PrimaryKeyJoinColumn[] pkColumnsAnn = null;
    JoinColumn[] joinColumnsAnn = null;
    if (uncastedColumn instanceof PrimaryKeyJoinColumn[]) {
        pkColumnsAnn = (PrimaryKeyJoinColumn[]) uncastedColumn;
    }
    if (uncastedColumn instanceof JoinColumn[]) {
        joinColumnsAnn = (JoinColumn[]) uncastedColumn;
    }
    if (pkColumnsAnn == null && joinColumnsAnn == null) {
        ejb3JoinColumns = new Ejb3JoinColumn[1];
        ejb3JoinColumns[0] = Ejb3JoinColumn.buildJoinColumn(null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
    } else {
        int nbrOfJoinColumns = pkColumnsAnn != null ? pkColumnsAnn.length : joinColumnsAnn.length;
        if (nbrOfJoinColumns == 0) {
            ejb3JoinColumns = new Ejb3JoinColumn[1];
            ejb3JoinColumns[0] = Ejb3JoinColumn.buildJoinColumn(null, null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
        } else {
            ejb3JoinColumns = new Ejb3JoinColumn[nbrOfJoinColumns];
            if (pkColumnsAnn != null) {
                for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) {
                    ejb3JoinColumns[colIndex] = Ejb3JoinColumn.buildJoinColumn(pkColumnsAnn[colIndex], null, persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
                }
            } else {
                for (int colIndex = 0; colIndex < nbrOfJoinColumns; colIndex++) {
                    ejb3JoinColumns[colIndex] = Ejb3JoinColumn.buildJoinColumn(null, joinColumnsAnn[colIndex], persistentClass.getIdentifier(), secondaryTables, propertyHolder, context);
                }
            }
        }
    }
    for (Ejb3JoinColumn joinColumn : ejb3JoinColumns) {
        joinColumn.forceNotNull();
    }
    bindJoinToPersistentClass(join, ejb3JoinColumns, context);
}
Also used : PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) Ejb3JoinColumn(org.hibernate.cfg.Ejb3JoinColumn)

Example 4 with PrimaryKeyJoinColumn

use of javax.persistence.PrimaryKeyJoinColumn in project hibernate-orm by hibernate.

the class Ejb3XmlOneToOneTest method testMultiplePrimaryKeyJoinColumn.

@Test
public void testMultiplePrimaryKeyJoinColumn() throws Exception {
    reader = getReader(Entity1.class, "field1", "one-to-one.orm3.xml");
    assertAnnotationPresent(OneToOne.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumn.class);
    assertAnnotationPresent(PrimaryKeyJoinColumns.class);
    assertAnnotationNotPresent(JoinColumns.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationNotPresent(JoinTable.class);
    PrimaryKeyJoinColumns joinColumnsAnno = reader.getAnnotation(PrimaryKeyJoinColumns.class);
    PrimaryKeyJoinColumn[] joinColumns = joinColumnsAnno.value();
    assertEquals(2, joinColumns.length);
    assertEquals("", joinColumns[0].name());
    assertEquals("", joinColumns[0].referencedColumnName());
    assertEquals("", joinColumns[0].columnDefinition());
    assertEquals("col1", joinColumns[1].name());
    assertEquals("col2", joinColumns[1].referencedColumnName());
    assertEquals("int", joinColumns[1].columnDefinition());
}
Also used : PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) Test(org.junit.Test)

Example 5 with PrimaryKeyJoinColumn

use of javax.persistence.PrimaryKeyJoinColumn in project CloudStack-archive by CloudStack-extras.

the class DbUtil method getPrimaryKeyJoinColumns.

public static PrimaryKeyJoinColumn[] getPrimaryKeyJoinColumns(Class<?> clazz) {
    PrimaryKeyJoinColumn pkjc = clazz.getAnnotation(PrimaryKeyJoinColumn.class);
    if (pkjc != null) {
        return new PrimaryKeyJoinColumn[] { pkjc };
    }
    PrimaryKeyJoinColumns pkjcs = clazz.getAnnotation(PrimaryKeyJoinColumns.class);
    if (pkjcs != null) {
        return pkjcs.value();
    }
    return null;
}
Also used : PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns)

Aggregations

PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)17 PrimaryKeyJoinColumns (javax.persistence.PrimaryKeyJoinColumns)8 ArrayList (java.util.ArrayList)4 Entity (javax.persistence.Entity)4 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)3 SecondaryTable (javax.persistence.SecondaryTable)3 Test (org.junit.Test)3 Date (java.util.Date)2 JoinColumn (javax.persistence.JoinColumn)2 JoinTable (javax.persistence.JoinTable)2 MappedSuperclass (javax.persistence.MappedSuperclass)2 Table (javax.persistence.Table)2 UniqueConstraint (javax.persistence.UniqueConstraint)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 AssociationOverrides (javax.persistence.AssociationOverrides)1 AttributeOverrides (javax.persistence.AttributeOverrides)1