Search in sources :

Example 1 with MapKeyColumn

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());
}
Also used : MapKeyColumn(jakarta.persistence.MapKeyColumn) Test(org.junit.Test)

Example 2 with MapKeyColumn

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();
}
Also used : InFlightMetadataCollector(org.hibernate.boot.spi.InFlightMetadataCollector) AssertionFailure(org.hibernate.AssertionFailure) SecondPass(org.hibernate.cfg.SecondPass) CollectionSecondPass(org.hibernate.cfg.CollectionSecondPass) MapKeyColumn(jakarta.persistence.MapKeyColumn) AnnotationException(org.hibernate.AnnotationException) ElementCollection(jakarta.persistence.ElementCollection) XClass(org.hibernate.annotations.common.reflection.XClass) Embeddable(jakarta.persistence.Embeddable)

Example 3 with MapKeyColumn

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);
            }
        }
    }
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) Selectable(org.hibernate.mapping.Selectable) MapKeyColumn(jakarta.persistence.MapKeyColumn) MapKeyJoinColumn(jakarta.persistence.MapKeyJoinColumn) Column(org.hibernate.mapping.Column) AnnotatedColumn(org.hibernate.cfg.AnnotatedColumn) AnnotatedJoinColumn(org.hibernate.cfg.AnnotatedJoinColumn) SimpleValue(org.hibernate.mapping.SimpleValue) Value(org.hibernate.mapping.Value) BasicValue(org.hibernate.mapping.BasicValue) DependantBasicValue(org.hibernate.mapping.DependantBasicValue) MapKeyColumn(jakarta.persistence.MapKeyColumn) OneToMany(org.hibernate.mapping.OneToMany) Map(java.util.Map) HashMap(java.util.HashMap) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 4 with MapKeyColumn

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());
}
Also used : MapKeyColumn(jakarta.persistence.MapKeyColumn) Test(org.junit.Test)

Example 5 with MapKeyColumn

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());
}
Also used : MapKeyColumn(jakarta.persistence.MapKeyColumn) Test(org.junit.Test)

Aggregations

MapKeyColumn (jakarta.persistence.MapKeyColumn)8 Test (org.junit.Test)6 AssertionFailure (org.hibernate.AssertionFailure)2 ElementCollection (jakarta.persistence.ElementCollection)1 Embeddable (jakarta.persistence.Embeddable)1 MapKeyJoinColumn (jakarta.persistence.MapKeyJoinColumn)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AnnotationException (org.hibernate.AnnotationException)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 InFlightMetadataCollector (org.hibernate.boot.spi.InFlightMetadataCollector)1 AnnotatedColumn (org.hibernate.cfg.AnnotatedColumn)1 AnnotatedJoinColumn (org.hibernate.cfg.AnnotatedJoinColumn)1 CollectionSecondPass (org.hibernate.cfg.CollectionSecondPass)1 SecondPass (org.hibernate.cfg.SecondPass)1 BasicValue (org.hibernate.mapping.BasicValue)1 Column (org.hibernate.mapping.Column)1 DependantBasicValue (org.hibernate.mapping.DependantBasicValue)1 OneToMany (org.hibernate.mapping.OneToMany)1 PersistentClass (org.hibernate.mapping.PersistentClass)1