Search in sources :

Example 1 with Comment

use of org.hibernate.annotations.Comment in project hibernate-orm by hibernate.

the class AnnotationBinder method bindCollection.

private static void bindCollection(PropertyHolder propertyHolder, Nullability nullability, PropertyData inferredData, Map<String, IdentifierGeneratorDefinition> classGenerators, EntityBinder entityBinder, boolean isIdentifierMapper, MetadataBuildingContext context, Map<XClass, InheritanceState> inheritanceStatePerClass, XProperty property, AnnotatedJoinColumn[] joinColumns) {
    OneToMany oneToManyAnn = property.getAnnotation(OneToMany.class);
    ManyToMany manyToManyAnn = property.getAnnotation(ManyToMany.class);
    ElementCollection elementCollectionAnn = property.getAnnotation(ElementCollection.class);
    if ((oneToManyAnn != null || manyToManyAnn != null || elementCollectionAnn != null) && isToManyAssociationWithinEmbeddableCollection(propertyHolder)) {
        throw new AnnotationException("@OneToMany, @ManyToMany or @ElementCollection cannot be used inside an @Embeddable that is also contained within an @ElementCollection: " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    if (property.isAnnotationPresent(OrderColumn.class) && manyToManyAnn != null && !manyToManyAnn.mappedBy().isEmpty()) {
        throw new AnnotationException("Explicit @OrderColumn on inverse side of @ManyToMany is illegal: " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    final IndexColumn indexColumn = IndexColumn.fromAnnotations(property.getAnnotation(OrderColumn.class), property.getAnnotation(org.hibernate.annotations.IndexColumn.class), property.getAnnotation(ListIndexBase.class), propertyHolder, inferredData, entityBinder.getSecondaryTables(), context);
    CollectionBinder collectionBinder = getCollectionBinder(property, hasMapKeyAnnotation(property), context);
    collectionBinder.setIndexColumn(indexColumn);
    collectionBinder.setMapKey(property.getAnnotation(MapKey.class));
    collectionBinder.setPropertyName(inferredData.getPropertyName());
    collectionBinder.setBatchSize(property.getAnnotation(BatchSize.class));
    collectionBinder.setJpaOrderBy(property.getAnnotation(jakarta.persistence.OrderBy.class));
    collectionBinder.setSqlOrderBy(getOverridableAnnotation(property, OrderBy.class, context));
    collectionBinder.setNaturalSort(property.getAnnotation(SortNatural.class));
    collectionBinder.setComparatorSort(property.getAnnotation(SortComparator.class));
    collectionBinder.setCache(property.getAnnotation(Cache.class));
    collectionBinder.setPropertyHolder(propertyHolder);
    Cascade hibernateCascade = property.getAnnotation(Cascade.class);
    NotFound notFound = property.getAnnotation(NotFound.class);
    collectionBinder.setIgnoreNotFound(notFound != null && notFound.action() == NotFoundAction.IGNORE);
    collectionBinder.setCollectionType(inferredData.getProperty().getElementClass());
    collectionBinder.setAccessType(inferredData.getDefaultAccess());
    AnnotatedColumn[] elementColumns;
    // do not use "element" if you are a JPA 2 @ElementCollection, only for legacy Hibernate mappings
    PropertyData virtualProperty = property.isAnnotationPresent(ElementCollection.class) ? inferredData : new WrappedInferredData(inferredData, "element");
    Comment comment = property.getAnnotation(Comment.class);
    if (property.isAnnotationPresent(Column.class)) {
        elementColumns = buildColumnFromAnnotation(property.getAnnotation(Column.class), comment, nullability, propertyHolder, virtualProperty, entityBinder.getSecondaryTables(), context);
    } else if (property.isAnnotationPresent(Formula.class)) {
        elementColumns = buildFormulaFromAnnotation(getOverridableAnnotation(property, Formula.class, context), comment, nullability, propertyHolder, virtualProperty, entityBinder.getSecondaryTables(), context);
    } else if (property.isAnnotationPresent(Columns.class)) {
        elementColumns = buildColumnsFromAnnotations(property.getAnnotation(Columns.class).columns(), comment, nullability, propertyHolder, virtualProperty, entityBinder.getSecondaryTables(), context);
    } else {
        elementColumns = buildColumnFromNoAnnotation(comment, nullability, propertyHolder, virtualProperty, entityBinder.getSecondaryTables(), context);
    }
    JoinColumn[] joinKeyColumns = mapKeyColumns(propertyHolder, inferredData, entityBinder, context, property, collectionBinder, comment);
    AnnotatedJoinColumn[] mapJoinColumns = buildJoinColumnsWithDefaultColumnSuffix(joinKeyColumns, comment, null, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), "_KEY", context);
    collectionBinder.setMapKeyManyToManyColumns(mapJoinColumns);
    // potential element
    collectionBinder.setEmbedded(property.isAnnotationPresent(Embedded.class));
    collectionBinder.setElementColumns(elementColumns);
    collectionBinder.setProperty(property);
    // TODO enhance exception with @ManyToAny and @CollectionOfElements
    if (oneToManyAnn != null && manyToManyAnn != null) {
        throw new AnnotationException("@OneToMany and @ManyToMany on the same property is not allowed: " + propertyHolder.getEntityName() + "." + inferredData.getPropertyName());
    }
    String mappedBy = null;
    ReflectionManager reflectionManager = context.getBootstrapContext().getReflectionManager();
    if (oneToManyAnn != null) {
        for (AnnotatedJoinColumn column : joinColumns) {
            if (column.isSecondary()) {
                throw new NotYetImplementedException("Collections having FK in secondary table");
            }
        }
        collectionBinder.setFkJoinColumns(joinColumns);
        mappedBy = oneToManyAnn.mappedBy();
        // noinspection unchecked
        collectionBinder.setTargetEntity(reflectionManager.toXClass(oneToManyAnn.targetEntity()));
        collectionBinder.setCascadeStrategy(getCascadeStrategy(oneToManyAnn.cascade(), hibernateCascade, oneToManyAnn.orphanRemoval(), false));
        collectionBinder.setOneToMany(true);
    } else if (elementCollectionAnn != null) {
        for (AnnotatedJoinColumn column : joinColumns) {
            if (column.isSecondary()) {
                throw new NotYetImplementedException("Collections having FK in secondary table");
            }
        }
        collectionBinder.setFkJoinColumns(joinColumns);
        mappedBy = "";
        final Class<?> targetElement = elementCollectionAnn.targetClass();
        collectionBinder.setTargetEntity(reflectionManager.toXClass(targetElement));
        // collectionBinder.setCascadeStrategy( getCascadeStrategy( embeddedCollectionAnn.cascade(), hibernateCascade ) );
        collectionBinder.setOneToMany(true);
    } else if (manyToManyAnn != null) {
        mappedBy = manyToManyAnn.mappedBy();
        // noinspection unchecked
        collectionBinder.setTargetEntity(reflectionManager.toXClass(manyToManyAnn.targetEntity()));
        collectionBinder.setCascadeStrategy(getCascadeStrategy(manyToManyAnn.cascade(), hibernateCascade, false, false));
        collectionBinder.setOneToMany(false);
    } else if (property.isAnnotationPresent(ManyToAny.class)) {
        mappedBy = "";
        collectionBinder.setTargetEntity(reflectionManager.toXClass(void.class));
        collectionBinder.setCascadeStrategy(getCascadeStrategy(null, hibernateCascade, false, false));
        collectionBinder.setOneToMany(false);
    }
    collectionBinder.setMappedBy(mappedBy);
    bindJoinedTableAssociation(property, context, entityBinder, collectionBinder, propertyHolder, inferredData, mappedBy);
    OnDelete onDeleteAnn = property.getAnnotation(OnDelete.class);
    boolean onDeleteCascade = onDeleteAnn != null && OnDeleteAction.CASCADE == onDeleteAnn.action();
    collectionBinder.setCascadeDeleteEnabled(onDeleteCascade);
    if (isIdentifierMapper) {
        collectionBinder.setInsertable(false);
        collectionBinder.setUpdatable(false);
    }
    if (property.isAnnotationPresent(CollectionId.class)) {
        // do not compute the generators unless necessary
        HashMap<String, IdentifierGeneratorDefinition> localGenerators = new HashMap<>(classGenerators);
        localGenerators.putAll(buildGenerators(property, context));
        collectionBinder.setLocalGenerators(localGenerators);
    }
    collectionBinder.setInheritanceStatePerClass(inheritanceStatePerClass);
    collectionBinder.setDeclaringClass(inferredData.getDeclaringClass());
    collectionBinder.bind();
}
Also used : HashMap(java.util.HashMap) SortNatural(org.hibernate.annotations.SortNatural) ManyToAny(org.hibernate.annotations.ManyToAny) Formula(org.hibernate.annotations.Formula) DiscriminatorFormula(org.hibernate.annotations.DiscriminatorFormula) MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) JoinColumn(jakarta.persistence.JoinColumn) PrimaryKeyJoinColumn(jakarta.persistence.PrimaryKeyJoinColumn) AnnotationException(org.hibernate.AnnotationException) SortComparator(org.hibernate.annotations.SortComparator) ElementCollection(jakarta.persistence.ElementCollection) Embedded(jakarta.persistence.Embedded) Cascade(org.hibernate.annotations.Cascade) OrderBy(org.hibernate.annotations.OrderBy) BatchSize(org.hibernate.annotations.BatchSize) Comment(org.hibernate.annotations.Comment) ReflectionManager(org.hibernate.annotations.common.reflection.ReflectionManager) OrderColumn(jakarta.persistence.OrderColumn) ManyToMany(jakarta.persistence.ManyToMany) AnnotatedJoinColumn.buildJoinTableJoinColumns(org.hibernate.cfg.AnnotatedJoinColumn.buildJoinTableJoinColumns) JoinColumns(jakarta.persistence.JoinColumns) PrimaryKeyJoinColumns(jakarta.persistence.PrimaryKeyJoinColumns) Columns(org.hibernate.annotations.Columns) MapKeyJoinColumns(jakarta.persistence.MapKeyJoinColumns) ListIndexBase(org.hibernate.annotations.ListIndexBase) OneToMany(jakarta.persistence.OneToMany) MapKey(jakarta.persistence.MapKey) IdentifierGeneratorDefinition(org.hibernate.boot.model.IdentifierGeneratorDefinition) XClass(org.hibernate.annotations.common.reflection.XClass) RootClass(org.hibernate.mapping.RootClass) PersistentClass(org.hibernate.mapping.PersistentClass) IdClass(jakarta.persistence.IdClass) CollectionBinder(org.hibernate.cfg.annotations.CollectionBinder) CollectionBinder.getCollectionBinder(org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder) Cache(org.hibernate.annotations.Cache) NotFound(org.hibernate.annotations.NotFound) OnDelete(org.hibernate.annotations.OnDelete)

Example 2 with Comment

use of org.hibernate.annotations.Comment in project hibernate-orm by hibernate.

the class EntityBinder method bindTable.

public void bindTable(String schema, String catalog, String tableName, List<UniqueConstraintHolder> uniqueConstraints, String constraints, InFlightMetadataCollector.EntityTableXref denormalizedSuperTableXref) {
    EntityTableNamingStrategyHelper namingStrategyHelper = new EntityTableNamingStrategyHelper(persistentClass.getClassName(), persistentClass.getEntityName(), name);
    final Identifier logicalName;
    if (StringHelper.isNotEmpty(tableName)) {
        logicalName = namingStrategyHelper.handleExplicitName(tableName, context);
    } else {
        logicalName = namingStrategyHelper.determineImplicitName(context);
    }
    final Table table = TableBinder.buildAndFillTable(schema, catalog, logicalName, persistentClass.isAbstract(), uniqueConstraints, null, constraints, context, this.subselect, denormalizedSuperTableXref);
    final RowId rowId = annotatedClass.getAnnotation(RowId.class);
    if (rowId != null) {
        table.setRowId(rowId.value());
    }
    final Comment comment = annotatedClass.getAnnotation(Comment.class);
    if (comment != null) {
        table.setComment(comment.value());
    }
    context.getMetadataCollector().addEntityTableXref(persistentClass.getEntityName(), logicalName, table, denormalizedSuperTableXref);
    if (persistentClass instanceof TableOwner) {
        LOG.debugf("Bind entity %s on table %s", persistentClass.getEntityName(), table.getName());
        ((TableOwner) persistentClass).setTable(table);
    } else {
        throw new AssertionFailure("binding a table for a subclass");
    }
}
Also used : TableOwner(org.hibernate.mapping.TableOwner) Comment(org.hibernate.annotations.Comment) RowId(org.hibernate.annotations.RowId) Identifier(org.hibernate.boot.model.naming.Identifier) SecondaryTable(jakarta.persistence.SecondaryTable) JoinTable(jakarta.persistence.JoinTable) Table(org.hibernate.mapping.Table) AssertionFailure(org.hibernate.AssertionFailure)

Example 3 with Comment

use of org.hibernate.annotations.Comment in project hibernate-orm by hibernate.

the class ColumnsBuilder method buildDefaultJoinColumnsForXToOne.

AnnotatedJoinColumn[] buildDefaultJoinColumnsForXToOne(XProperty property, PropertyData inferredData) {
    AnnotatedJoinColumn[] joinColumns;
    JoinTable joinTableAnn = propertyHolder.getJoinTable(property);
    Comment comment = property.getAnnotation(Comment.class);
    if (joinTableAnn != null) {
        joinColumns = AnnotatedJoinColumn.buildJoinColumns(joinTableAnn.inverseJoinColumns(), comment, null, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
        if (StringHelper.isEmpty(joinTableAnn.name())) {
            throw new AnnotationException("JoinTable.name() on a @ToOne association has to be explicit: " + BinderHelper.getPath(propertyHolder, inferredData));
        }
    } else {
        OneToOne oneToOneAnn = property.getAnnotation(OneToOne.class);
        String mappedBy = oneToOneAnn != null ? oneToOneAnn.mappedBy() : null;
        joinColumns = AnnotatedJoinColumn.buildJoinColumns(null, comment, mappedBy, entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
    }
    return joinColumns;
}
Also used : Comment(org.hibernate.annotations.Comment) OneToOne(jakarta.persistence.OneToOne) AnnotationException(org.hibernate.AnnotationException) JoinTable(jakarta.persistence.JoinTable)

Example 4 with Comment

use of org.hibernate.annotations.Comment in project hibernate-orm by hibernate.

the class ColumnsBuilder method extractMetadata.

public ColumnsBuilder extractMetadata() {
    columns = null;
    joinColumns = buildExplicitJoinColumns(property, inferredData);
    Comment comment = property.getAnnotation(Comment.class);
    if (property.isAnnotationPresent(Column.class)) {
        columns = buildColumnFromAnnotation(property.getAnnotation(Column.class), comment, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
    } else if (property.isAnnotationPresent(Formula.class)) {
        columns = buildFormulaFromAnnotation(getOverridableAnnotation(property, Formula.class, buildingContext), comment, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
    } else if (property.isAnnotationPresent(Columns.class)) {
        columns = buildColumnsFromAnnotations(property.getAnnotation(Columns.class).columns(), comment, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
    }
    // set default values if needed
    if (joinColumns == null && (property.isAnnotationPresent(ManyToOne.class) || property.isAnnotationPresent(OneToOne.class))) {
        joinColumns = buildDefaultJoinColumnsForXToOne(property, inferredData);
    } else if (joinColumns == null && (property.isAnnotationPresent(OneToMany.class) || property.isAnnotationPresent(ElementCollection.class))) {
        OneToMany oneToMany = property.getAnnotation(OneToMany.class);
        joinColumns = AnnotatedJoinColumn.buildJoinColumns(null, comment, oneToMany != null ? oneToMany.mappedBy() : "", entityBinder.getSecondaryTables(), propertyHolder, inferredData.getPropertyName(), buildingContext);
    } else if (joinColumns == null && property.isAnnotationPresent(org.hibernate.annotations.Any.class)) {
        throw new AnnotationException("@Any requires an explicit @JoinColumn(s): " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    if (columns == null && !property.isAnnotationPresent(ManyToMany.class)) {
        // useful for collection of embedded elements
        columns = buildColumnFromNoAnnotation(comment, nullability, propertyHolder, inferredData, entityBinder.getSecondaryTables(), buildingContext);
    }
    if (nullability == Nullability.FORCED_NOT_NULL) {
        // force columns to not null
        for (AnnotatedColumn col : columns) {
            col.forceNotNull();
        }
    }
    return this;
}
Also used : Comment(org.hibernate.annotations.Comment) Formula(org.hibernate.annotations.Formula) JoinColumnOrFormula(org.hibernate.annotations.JoinColumnOrFormula) JoinFormula(org.hibernate.annotations.JoinFormula) OneToOne(jakarta.persistence.OneToOne) Columns(org.hibernate.annotations.Columns) JoinColumns(jakarta.persistence.JoinColumns) AnnotationException(org.hibernate.AnnotationException) OneToMany(jakarta.persistence.OneToMany) ManyToOne(jakarta.persistence.ManyToOne)

Aggregations

Comment (org.hibernate.annotations.Comment)4 AnnotationException (org.hibernate.AnnotationException)3 JoinColumns (jakarta.persistence.JoinColumns)2 JoinTable (jakarta.persistence.JoinTable)2 OneToMany (jakarta.persistence.OneToMany)2 OneToOne (jakarta.persistence.OneToOne)2 Columns (org.hibernate.annotations.Columns)2 Formula (org.hibernate.annotations.Formula)2 ElementCollection (jakarta.persistence.ElementCollection)1 Embedded (jakarta.persistence.Embedded)1 IdClass (jakarta.persistence.IdClass)1 JoinColumn (jakarta.persistence.JoinColumn)1 ManyToMany (jakarta.persistence.ManyToMany)1 ManyToOne (jakarta.persistence.ManyToOne)1 MapKey (jakarta.persistence.MapKey)1 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)1 MapKeyJoinColumns (jakarta.persistence.MapKeyJoinColumns)1 OrderColumn (jakarta.persistence.OrderColumn)1 PrimaryKeyJoinColumn (jakarta.persistence.PrimaryKeyJoinColumn)1 PrimaryKeyJoinColumns (jakarta.persistence.PrimaryKeyJoinColumns)1