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);
}
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();
}
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());
}
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());
}
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());
}
Aggregations