Search in sources :

Example 6 with AttributeOverride

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

the class Ejb3XmlElementCollectionTest method testSingleMapKeyAttributeOverride.

/**
 * When there's a single map key attribute override, we still wrap it with
 * an AttributeOverrides annotation.
 */
@Test
public void testSingleMapKeyAttributeOverride() throws Exception {
    reader = getReader(Entity3.class, "field1", "element-collection.orm10.xml");
    assertAnnotationPresent(ElementCollection.class);
    assertAnnotationNotPresent(MapKey.class);
    assertAnnotationNotPresent(MapKeyClass.class);
    assertAnnotationNotPresent(MapKeyTemporal.class);
    assertAnnotationNotPresent(MapKeyEnumerated.class);
    assertAnnotationNotPresent(MapKeyColumn.class);
    assertAnnotationNotPresent(MapKeyJoinColumns.class);
    assertAnnotationNotPresent(MapKeyJoinColumn.class);
    assertAnnotationNotPresent(AttributeOverride.class);
    assertAnnotationPresent(AttributeOverrides.class);
    AttributeOverrides overridesAnno = reader.getAnnotation(AttributeOverrides.class);
    AttributeOverride[] overrides = overridesAnno.value();
    assertEquals(1, overrides.length);
    assertEquals("field1", overrides[0].name());
    assertEquals("col1", overrides[0].column().name());
}
Also used : AttributeOverrides(jakarta.persistence.AttributeOverrides) AttributeOverride(jakarta.persistence.AttributeOverride) Test(org.junit.Test)

Example 7 with AttributeOverride

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

the class Ejb3XmlElementCollectionTest method testMixedAttributeOverrides.

/**
 * Tests that map-key-attribute-override and attribute-override elements
 * both end up in the AttributeOverrides annotation.
 */
@Test
public void testMixedAttributeOverrides() throws Exception {
    reader = getReader(Entity3.class, "field1", "element-collection.orm23.xml");
    assertAnnotationPresent(ElementCollection.class);
    assertAnnotationNotPresent(AttributeOverride.class);
    assertAnnotationPresent(AttributeOverrides.class);
    AttributeOverrides overridesAnno = reader.getAnnotation(AttributeOverrides.class);
    AttributeOverride[] overrides = overridesAnno.value();
    assertEquals(2, overrides.length);
    assertEquals("field1", overrides[0].name());
    assertEquals("col1", overrides[0].column().name());
    assertEquals("field2", overrides[1].name());
    assertEquals("col2", overrides[1].column().name());
}
Also used : AttributeOverrides(jakarta.persistence.AttributeOverrides) AttributeOverride(jakarta.persistence.AttributeOverride) Test(org.junit.Test)

Example 8 with AttributeOverride

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

the class Ejb3XmlOneToManyTest method testMultipleMapKeyAttributeOverrides.

@Test
public void testMultipleMapKeyAttributeOverrides() throws Exception {
    reader = getReader(Entity3.class, "field1", "one-to-many.orm11.xml");
    assertAnnotationPresent(OneToMany.class);
    assertAnnotationNotPresent(MapKey.class);
    assertAnnotationNotPresent(MapKeyClass.class);
    assertAnnotationNotPresent(MapKeyTemporal.class);
    assertAnnotationNotPresent(MapKeyEnumerated.class);
    assertAnnotationNotPresent(MapKeyColumn.class);
    assertAnnotationNotPresent(MapKeyJoinColumns.class);
    assertAnnotationNotPresent(MapKeyJoinColumn.class);
    assertAnnotationNotPresent(AttributeOverride.class);
    assertAnnotationPresent(AttributeOverrides.class);
    AttributeOverrides overridesAnno = reader.getAnnotation(AttributeOverrides.class);
    AttributeOverride[] overrides = overridesAnno.value();
    assertEquals(2, overrides.length);
    assertEquals("field1", overrides[0].name());
    assertEquals("", overrides[0].column().name());
    assertFalse(overrides[0].column().unique());
    assertTrue(overrides[0].column().nullable());
    assertTrue(overrides[0].column().insertable());
    assertTrue(overrides[0].column().updatable());
    assertEquals("", overrides[0].column().columnDefinition());
    assertEquals("", overrides[0].column().table());
    assertEquals(255, overrides[0].column().length());
    assertEquals(0, overrides[0].column().precision());
    assertEquals(0, overrides[0].column().scale());
    assertEquals("field2", overrides[1].name());
    assertEquals("col1", overrides[1].column().name());
    assertTrue(overrides[1].column().unique());
    assertFalse(overrides[1].column().nullable());
    assertFalse(overrides[1].column().insertable());
    assertFalse(overrides[1].column().updatable());
    assertEquals("int", overrides[1].column().columnDefinition());
    assertEquals("table1", overrides[1].column().table());
    assertEquals(50, overrides[1].column().length());
    assertEquals(2, overrides[1].column().precision());
    assertEquals(1, overrides[1].column().scale());
}
Also used : AttributeOverrides(jakarta.persistence.AttributeOverrides) AttributeOverride(jakarta.persistence.AttributeOverride) Test(org.junit.Test)

Example 9 with AttributeOverride

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

the class CollectionBinder method handleElementCollection.

private void handleElementCollection(Collection collValue, AnnotatedColumn[] elementColumns, boolean isEmbedded, XClass collType, XProperty property, PropertyHolder parentPropertyHolder, MetadataBuildingContext buildingContext, String hqlOrderBy) {
    XClass elementClass;
    AnnotatedClassType classType;
    CollectionPropertyHolder holder;
    if (BinderHelper.PRIMITIVE_NAMES.contains(collType.getName())) {
        classType = AnnotatedClassType.NONE;
        elementClass = null;
        holder = PropertyHolderBuilder.buildPropertyHolder(collValue, collValue.getRole(), null, property, parentPropertyHolder, buildingContext);
    } else {
        elementClass = collType;
        classType = buildingContext.getMetadataCollector().getClassType(elementClass);
        holder = PropertyHolderBuilder.buildPropertyHolder(collValue, collValue.getRole(), elementClass, property, parentPropertyHolder, buildingContext);
        // 'parentPropertyHolder' is the PropertyHolder for the owner of the collection
        // 'holder' is the CollectionPropertyHolder.
        // 'property' is the collection XProperty
        parentPropertyHolder.startingProperty(property);
        // force in case of attribute override
        boolean attributeOverride = property.isAnnotationPresent(AttributeOverride.class) || property.isAnnotationPresent(AttributeOverrides.class);
        // todo : force in the case of Convert annotation(s) with embedded paths (beyond key/value prefixes)?
        if (isEmbedded || attributeOverride) {
            classType = AnnotatedClassType.EMBEDDABLE;
        }
    }
    final Class<? extends CompositeUserType<?>> compositeUserType = resolveCompositeUserType(property, elementClass, buildingContext);
    if (AnnotatedClassType.EMBEDDABLE == classType || compositeUserType != null) {
        holder.prepare(property);
        EntityBinder entityBinder = new EntityBinder();
        PersistentClass owner = collValue.getOwner();
        final AccessType baseAccessType;
        final Access accessAnn = property.getAnnotation(Access.class);
        if (accessAnn != null) {
            // the attribute is locally annotated with `@Access`, use that
            baseAccessType = accessAnn.value() == PROPERTY ? AccessType.PROPERTY : AccessType.FIELD;
        } else if (owner.getIdentifierProperty() != null) {
            // use the access for the owning entity's id attribute, if one
            baseAccessType = owner.getIdentifierProperty().getPropertyAccessorName().equals("property") ? AccessType.PROPERTY : AccessType.FIELD;
        } else if (owner.getIdentifierMapper() != null && owner.getIdentifierMapper().getPropertySpan() > 0) {
            // use the access for the owning entity's "id mapper", if one
            Property prop = owner.getIdentifierMapper().getProperties().get(0);
            baseAccessType = prop.getPropertyAccessorName().equals("property") ? AccessType.PROPERTY : AccessType.FIELD;
        } else {
            // otherwise...
            throw new AssertionFailure("Unable to guess collection property accessor name");
        }
        // TODO be smart with isNullable
        Component component = fillComponent(holder, getSpecialMembers(elementClass), baseAccessType, true, entityBinder, false, false, true, resolveCustomInstantiator(property, elementClass, buildingContext), compositeUserType, buildingContext, inheritanceStatePerClass);
        collValue.setElement(component);
        if (StringHelper.isNotEmpty(hqlOrderBy)) {
            String orderBy = adjustUserSuppliedValueCollectionOrderingFragment(hqlOrderBy);
            if (orderBy != null) {
                collValue.setOrderBy(orderBy);
            }
        }
    } else {
        holder.prepare(property);
        final BasicValueBinder elementBinder = new BasicValueBinder(BasicValueBinder.Kind.COLLECTION_ELEMENT, buildingContext);
        elementBinder.setReturnedClassName(collType.getName());
        if (elementColumns == null || elementColumns.length == 0) {
            elementColumns = new AnnotatedColumn[1];
            AnnotatedColumn column = new AnnotatedColumn();
            column.setImplicit(false);
            // not following the spec but more clean
            column.setNullable(true);
            column.setLogicalColumnName(Collection.DEFAULT_ELEMENT_COLUMN_NAME);
            // TODO create an EMPTY_JOINS collection
            column.setJoins(new HashMap<>());
            column.setBuildingContext(buildingContext);
            column.bind();
            elementColumns[0] = column;
        }
        // override the table
        for (AnnotatedColumn column : elementColumns) {
            column.setTable(collValue.getCollectionTable());
        }
        elementBinder.setColumns(elementColumns);
        elementBinder.setType(property, elementClass, collValue.getOwnerEntityName(), holder.resolveElementAttributeConverterDescriptor(property, elementClass));
        elementBinder.setPersistentClassName(propertyHolder.getEntityName());
        elementBinder.setAccessType(accessType);
        collValue.setElement(elementBinder.make());
        String orderBy = adjustUserSuppliedValueCollectionOrderingFragment(hqlOrderBy);
        if (orderBy != null) {
            collValue.setOrderBy(orderBy);
        }
    }
}
Also used : CollectionPropertyHolder(org.hibernate.cfg.CollectionPropertyHolder) AssertionFailure(org.hibernate.AssertionFailure) Access(jakarta.persistence.Access) XClass(org.hibernate.annotations.common.reflection.XClass) AttributeOverride(jakarta.persistence.AttributeOverride) AttributeOverrides(jakarta.persistence.AttributeOverrides) AnnotatedColumn(org.hibernate.cfg.AnnotatedColumn) AnnotationBinder.fillComponent(org.hibernate.cfg.AnnotationBinder.fillComponent) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty) AnnotatedClassType(org.hibernate.cfg.AnnotatedClassType) AccessType(org.hibernate.cfg.AccessType) PersistentClass(org.hibernate.mapping.PersistentClass)

Example 10 with AttributeOverride

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

the class JPAXMLOverriddenAnnotationReader method buildAttributeOverrides.

private List<AttributeOverride> buildAttributeOverrides(List<JaxbAttributeOverride> subelements, String nodeName) {
    List<AttributeOverride> overrides = new ArrayList<>();
    if (subelements != null && subelements.size() > 0) {
        for (JaxbAttributeOverride current : subelements) {
            AnnotationDescriptor override = new AnnotationDescriptor(AttributeOverride.class);
            copyAttribute(override, "name", current.getName(), true);
            JaxbColumn column = current.getColumn();
            override.setValue("column", getColumn(column, true, nodeName));
            overrides.add(AnnotationFactory.create(override));
        }
    }
    return overrides;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) JaxbAttributeOverride(org.hibernate.boot.jaxb.mapping.spi.JaxbAttributeOverride) ArrayList(java.util.ArrayList) JaxbColumn(org.hibernate.boot.jaxb.mapping.spi.JaxbColumn) JaxbAttributeOverride(org.hibernate.boot.jaxb.mapping.spi.JaxbAttributeOverride) AttributeOverride(jakarta.persistence.AttributeOverride)

Aggregations

AttributeOverride (jakarta.persistence.AttributeOverride)15 AttributeOverrides (jakarta.persistence.AttributeOverrides)12 Test (org.junit.Test)9 JaxbAttributeOverride (org.hibernate.boot.jaxb.mapping.spi.JaxbAttributeOverride)4 ArrayList (java.util.ArrayList)3 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)3 Access (jakarta.persistence.Access)1 Column (jakarta.persistence.Column)1 JoinColumn (jakarta.persistence.JoinColumn)1 Annotation (java.lang.annotation.Annotation)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AssertionFailure (org.hibernate.AssertionFailure)1 XClass (org.hibernate.annotations.common.reflection.XClass)1 XProperty (org.hibernate.annotations.common.reflection.XProperty)1 JaxbColumn (org.hibernate.boot.jaxb.mapping.spi.JaxbColumn)1 JaxbElementCollection (org.hibernate.boot.jaxb.mapping.spi.JaxbElementCollection)1 AccessType (org.hibernate.cfg.AccessType)1 AnnotatedClassType (org.hibernate.cfg.AnnotatedClassType)1