Search in sources :

Example 1 with OrderColumn

use of jakarta.persistence.OrderColumn in project hibernate-orm by hibernate.

the class ListMappingTest method testOrderColumnInNormalBiDirectonalModel.

@Test
public void testOrderColumnInNormalBiDirectonalModel() {
    Metadata metadata = new MetadataSources(ssr).addAnnotatedClass(Order.class).addAnnotatedClass(LineItem.class).buildMetadata();
    Collection lineItemsBinding = metadata.getCollectionBindings().iterator().next();
    // make sure it was interpreted as a List (aka, as having an OrderColumn at all)
    assertThat(lineItemsBinding, instanceOf(org.hibernate.mapping.List.class));
    org.hibernate.mapping.List asList = (org.hibernate.mapping.List) lineItemsBinding;
    // assert the OrderColumn details
    final Column positionColumn = (Column) asList.getIndex().getColumnIterator().next();
    assertThat(positionColumn.getName(), equalTo("position"));
    // make sure the OrderColumn is part of the collection table
    assertTrue(asList.getCollectionTable().containsColumn(positionColumn));
    class TargetImpl extends GenerationTargetToStdout {

        boolean found = false;

        @Override
        public void accept(String action) {
            super.accept(action);
            if (action.matches("^create( (column|row))? table t_line_item.+")) {
                if (action.contains("position")) {
                    found = true;
                }
            }
        }
    }
    TargetImpl target = new TargetImpl();
    new SchemaCreatorImpl(ssr).doCreation(metadata, true, target);
    assertTrue(target.found);
}
Also used : GenerationTargetToStdout(org.hibernate.tool.schema.internal.exec.GenerationTargetToStdout) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) SchemaCreatorImpl(org.hibernate.tool.schema.internal.SchemaCreatorImpl) Column(org.hibernate.mapping.Column) JoinColumn(jakarta.persistence.JoinColumn) OrderColumn(jakarta.persistence.OrderColumn) Collection(org.hibernate.mapping.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 2 with OrderColumn

use of jakarta.persistence.OrderColumn 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 3 with OrderColumn

use of jakarta.persistence.OrderColumn in project hibernate-orm by hibernate.

the class Ejb3XmlElementCollectionTest method testOrderColumnNoAttributes.

@Test
public void testOrderColumnNoAttributes() throws Exception {
    reader = getReader(Entity2.class, "field1", "element-collection.orm3.xml");
    assertAnnotationPresent(ElementCollection.class);
    assertAnnotationNotPresent(OrderBy.class);
    assertAnnotationPresent(OrderColumn.class);
    OrderColumn orderColumnAnno = reader.getAnnotation(OrderColumn.class);
    assertEquals("", orderColumnAnno.columnDefinition());
    assertEquals("", orderColumnAnno.name());
    assertTrue(orderColumnAnno.insertable());
    assertTrue(orderColumnAnno.nullable());
    assertTrue(orderColumnAnno.updatable());
}
Also used : OrderColumn(jakarta.persistence.OrderColumn) Test(org.junit.Test)

Example 4 with OrderColumn

use of jakarta.persistence.OrderColumn in project hibernate-orm by hibernate.

the class Ejb3XmlManyToManyTest method testOrderColumnNoAttributes.

@Test
public void testOrderColumnNoAttributes() throws Exception {
    reader = getReader(Entity2.class, "field1", "many-to-many.orm3.xml");
    assertAnnotationPresent(ManyToMany.class);
    assertAnnotationNotPresent(OrderBy.class);
    assertAnnotationPresent(OrderColumn.class);
    OrderColumn orderColumnAnno = reader.getAnnotation(OrderColumn.class);
    assertEquals("", orderColumnAnno.columnDefinition());
    assertEquals("", orderColumnAnno.name());
    assertTrue(orderColumnAnno.insertable());
    assertTrue(orderColumnAnno.nullable());
    assertTrue(orderColumnAnno.updatable());
}
Also used : OrderColumn(jakarta.persistence.OrderColumn) Test(org.junit.Test)

Example 5 with OrderColumn

use of jakarta.persistence.OrderColumn in project hibernate-orm by hibernate.

the class Ejb3XmlManyToManyTest method testOrderColumnAllAttributes.

@Test
public void testOrderColumnAllAttributes() throws Exception {
    reader = getReader(Entity2.class, "field1", "many-to-many.orm4.xml");
    assertAnnotationPresent(ManyToMany.class);
    assertAnnotationNotPresent(OrderBy.class);
    assertAnnotationPresent(OrderColumn.class);
    OrderColumn orderColumnAnno = reader.getAnnotation(OrderColumn.class);
    assertEquals("int", orderColumnAnno.columnDefinition());
    assertEquals("col1", orderColumnAnno.name());
    assertFalse(orderColumnAnno.insertable());
    assertFalse(orderColumnAnno.nullable());
    assertFalse(orderColumnAnno.updatable());
}
Also used : OrderColumn(jakarta.persistence.OrderColumn) Test(org.junit.Test)

Aggregations

OrderColumn (jakarta.persistence.OrderColumn)9 Test (org.junit.Test)6 ElementCollection (jakarta.persistence.ElementCollection)2 JoinColumn (jakarta.persistence.JoinColumn)2 ManyToMany (jakarta.persistence.ManyToMany)2 OneToMany (jakarta.persistence.OneToMany)2 OrderBy (org.hibernate.annotations.OrderBy)2 Embedded (jakarta.persistence.Embedded)1 IdClass (jakarta.persistence.IdClass)1 JoinColumns (jakarta.persistence.JoinColumns)1 MapKey (jakarta.persistence.MapKey)1 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)1 MapKeyJoinColumns (jakarta.persistence.MapKeyJoinColumns)1 PrimaryKeyJoinColumn (jakarta.persistence.PrimaryKeyJoinColumn)1 PrimaryKeyJoinColumns (jakarta.persistence.PrimaryKeyJoinColumns)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AnnotationException (org.hibernate.AnnotationException)1 BatchSize (org.hibernate.annotations.BatchSize)1