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