use of jakarta.persistence.MapKeyColumn in project hibernate-orm by hibernate.
the class Ejb3XmlOneToManyTest method testMapKeyColumnNoAttributes.
@Test
public void testMapKeyColumnNoAttributes() throws Exception {
reader = getReader(Entity3.class, "field1", "one-to-many.orm12.xml");
assertAnnotationPresent(OneToMany.class);
assertAnnotationNotPresent(MapKey.class);
assertAnnotationNotPresent(MapKeyClass.class);
assertAnnotationNotPresent(MapKeyTemporal.class);
assertAnnotationNotPresent(MapKeyEnumerated.class);
assertAnnotationPresent(MapKeyColumn.class);
assertAnnotationNotPresent(MapKeyJoinColumns.class);
assertAnnotationNotPresent(MapKeyJoinColumn.class);
MapKeyColumn keyColAnno = reader.getAnnotation(MapKeyColumn.class);
assertEquals("", keyColAnno.columnDefinition());
assertEquals("", keyColAnno.name());
assertEquals("", keyColAnno.table());
assertFalse(keyColAnno.nullable());
assertTrue(keyColAnno.insertable());
assertFalse(keyColAnno.unique());
assertTrue(keyColAnno.updatable());
assertEquals(255, keyColAnno.length());
assertEquals(0, keyColAnno.precision());
assertEquals(0, keyColAnno.scale());
}
use of jakarta.persistence.MapKeyColumn in project hibernate-orm by hibernate.
the class CollectionBinder method bind.
public void bind() {
this.collection = createCollection(propertyHolder.getPersistentClass());
String role = StringHelper.qualify(propertyHolder.getPath(), propertyName);
LOG.debugf("Collection role: %s", role);
collection.setRole(role);
collection.setMappedByProperty(mappedBy);
if (property.isAnnotationPresent(MapKeyColumn.class) && mapKeyPropertyName != null) {
throw new AnnotationException("Cannot mix @jakarta.persistence.MapKey and @MapKeyColumn or @org.hibernate.annotations.MapKey " + "on the same collection: " + StringHelper.qualify(propertyHolder.getPath(), propertyName));
}
bindExplicitTypes();
// set laziness
defineFetchingStrategy();
collection.setBatchSize(batchSize);
collection.setMutable(!property.isAnnotationPresent(Immutable.class));
// work on association
boolean isMappedBy = !isEmptyAnnotationValue(mappedBy);
bindOptimisticLock(isMappedBy);
bindCustomPersister();
applySortingAndOrdering(collection);
bindCache();
bindLoader();
detectMappedByProblem(isMappedBy);
collection.setInverse(isMappedBy);
final InFlightMetadataCollector metadataCollector = buildingContext.getMetadataCollector();
// many to many may need some second pass information
if (!oneToMany && isMappedBy) {
metadataCollector.addMappedBy(getCollectionType().getName(), mappedBy, propertyName);
}
// TODO reduce tableBinder != null and oneToMany
XClass collectionType = getCollectionType();
if (inheritanceStatePerClass == null) {
throw new AssertionFailure("inheritanceStatePerClass not set");
}
SecondPass sp = getSecondPass(fkJoinColumns, joinColumns, inverseJoinColumns, elementColumns, mapKeyColumns, mapKeyManyToManyColumns, isEmbedded, property, collectionType, ignoreNotFound, oneToMany, tableBinder, buildingContext);
if (collectionType.isAnnotationPresent(Embeddable.class) || // JPA 2
property.isAnnotationPresent(ElementCollection.class)) {
// do it right away, otherwise @ManyToOne on composite element call addSecondPass
// and raise a ConcurrentModificationException
// sp.doSecondPass( CollectionHelper.EMPTY_MAP );
metadataCollector.addSecondPass(sp, !isMappedBy);
} else {
metadataCollector.addSecondPass(sp, !isMappedBy);
}
metadataCollector.addCollectionBinding(collection);
bindProperty();
}
use of jakarta.persistence.MapKeyColumn in project hibernate-orm by hibernate.
the class MapBinder method makeOneToManyMapKeyColumnNullableIfNotInProperty.
private void makeOneToManyMapKeyColumnNullableIfNotInProperty(final XProperty property) {
final org.hibernate.mapping.Map map = (org.hibernate.mapping.Map) this.collection;
if (map.isOneToMany() && property.isAnnotationPresent(MapKeyColumn.class)) {
final Value indexValue = map.getIndex();
if (indexValue.getColumnSpan() != 1) {
throw new AssertionFailure("Map key mapped by @MapKeyColumn does not have 1 column");
}
final Selectable selectable = indexValue.getSelectables().get(0);
if (selectable.isFormula()) {
throw new AssertionFailure("Map key mapped by @MapKeyColumn is a Formula");
}
Column column = (Column) selectable;
if (!column.isNullable()) {
final PersistentClass persistentClass = ((OneToMany) map.getElement()).getAssociatedClass();
// need to check "un-joined" properties.
if (!propertiesContainColumn(persistentClass.getUnjoinedProperties(), column)) {
// The index column is not mapped to an associated entity property so we can
// safely make the index column nullable.
column.setNullable(true);
}
}
}
}
use of jakarta.persistence.MapKeyColumn in project hibernate-orm by hibernate.
the class Ejb3XmlElementCollectionTest method testMapKeyColumnNoAttributes.
@Test
public void testMapKeyColumnNoAttributes() throws Exception {
reader = getReader(Entity3.class, "field1", "element-collection.orm12.xml");
assertAnnotationPresent(ElementCollection.class);
assertAnnotationNotPresent(MapKey.class);
assertAnnotationNotPresent(MapKeyClass.class);
assertAnnotationNotPresent(MapKeyTemporal.class);
assertAnnotationNotPresent(MapKeyEnumerated.class);
assertAnnotationPresent(MapKeyColumn.class);
assertAnnotationNotPresent(MapKeyJoinColumns.class);
assertAnnotationNotPresent(MapKeyJoinColumn.class);
MapKeyColumn keyColAnno = reader.getAnnotation(MapKeyColumn.class);
assertEquals("", keyColAnno.columnDefinition());
assertEquals("", keyColAnno.name());
assertEquals("", keyColAnno.table());
assertFalse(keyColAnno.nullable());
assertTrue(keyColAnno.insertable());
assertFalse(keyColAnno.unique());
assertTrue(keyColAnno.updatable());
assertEquals(255, keyColAnno.length());
assertEquals(0, keyColAnno.precision());
assertEquals(0, keyColAnno.scale());
}
use of jakarta.persistence.MapKeyColumn in project hibernate-orm by hibernate.
the class Ejb3XmlElementCollectionTest method testMapKeyColumnAllAttributes.
@Test
public void testMapKeyColumnAllAttributes() throws Exception {
reader = getReader(Entity3.class, "field1", "element-collection.orm13.xml");
assertAnnotationPresent(ElementCollection.class);
assertAnnotationNotPresent(MapKey.class);
assertAnnotationNotPresent(MapKeyClass.class);
assertAnnotationNotPresent(MapKeyTemporal.class);
assertAnnotationNotPresent(MapKeyEnumerated.class);
assertAnnotationPresent(MapKeyColumn.class);
assertAnnotationNotPresent(MapKeyJoinColumns.class);
assertAnnotationNotPresent(MapKeyJoinColumn.class);
MapKeyColumn keyColAnno = reader.getAnnotation(MapKeyColumn.class);
assertEquals("int", keyColAnno.columnDefinition());
assertEquals("col1", keyColAnno.name());
assertEquals("table1", keyColAnno.table());
assertTrue(keyColAnno.nullable());
assertFalse(keyColAnno.insertable());
assertTrue(keyColAnno.unique());
assertFalse(keyColAnno.updatable());
assertEquals(50, keyColAnno.length());
assertEquals(2, keyColAnno.precision());
assertEquals(1, keyColAnno.scale());
}
Aggregations