Search in sources :

Example 1 with AssociationOverride

use of javax.persistence.AssociationOverride in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method addAssociationOverrideIfNeeded.

private void addAssociationOverrideIfNeeded(AssociationOverride annotation, List<AssociationOverride> overrides) {
    if (annotation != null) {
        String overrideName = annotation.name();
        boolean present = false;
        for (AssociationOverride current : overrides) {
            if (current.name().equals(overrideName)) {
                present = true;
                break;
            }
        }
        if (!present) {
            overrides.add(annotation);
        }
    }
}
Also used : AssociationOverride(javax.persistence.AssociationOverride)

Example 2 with AssociationOverride

use of javax.persistence.AssociationOverride in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method getAssociationOverrides.

/**
	 * @param mergeWithAnnotations Whether to use Java annotations for this
	 * element, if present and not disabled by the XMLContext defaults.
	 * In some contexts (such as an element-collection mapping) merging
	 * with annotations is never allowed.
	 */
private AssociationOverrides getAssociationOverrides(Element tree, XMLContext.Default defaults, boolean mergeWithAnnotations) {
    List<AssociationOverride> attributes = buildAssociationOverrides(tree, defaults);
    if (mergeWithAnnotations && defaults.canUseJavaAnnotations()) {
        AssociationOverride annotation = getPhysicalAnnotation(AssociationOverride.class);
        addAssociationOverrideIfNeeded(annotation, attributes);
        AssociationOverrides annotations = getPhysicalAnnotation(AssociationOverrides.class);
        if (annotations != null) {
            for (AssociationOverride current : annotations.value()) {
                addAssociationOverrideIfNeeded(current, attributes);
            }
        }
    }
    if (attributes.size() > 0) {
        AnnotationDescriptor ad = new AnnotationDescriptor(AssociationOverrides.class);
        ad.setValue("value", attributes.toArray(new AssociationOverride[attributes.size()]));
        return AnnotationFactory.create(ad);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AssociationOverrides(javax.persistence.AssociationOverrides) AssociationOverride(javax.persistence.AssociationOverride)

Example 3 with AssociationOverride

use of javax.persistence.AssociationOverride in project hibernate-orm by hibernate.

the class Ejb3XmlElementCollectionTest method testSingleAssociationOverride.

/**
	 * When there's a single association override, we still wrap it with an
	 * AssociationOverrides annotation.
	 */
@Test
public void testSingleAssociationOverride() throws Exception {
    reader = getReader(Entity3.class, "field1", "element-collection.orm24.xml");
    assertAnnotationPresent(ElementCollection.class);
    assertAnnotationNotPresent(AssociationOverride.class);
    assertAnnotationPresent(AssociationOverrides.class);
    AssociationOverrides overridesAnno = reader.getAnnotation(AssociationOverrides.class);
    AssociationOverride[] overrides = overridesAnno.value();
    assertEquals(1, overrides.length);
    assertEquals("association1", overrides[0].name());
    assertEquals(0, overrides[0].joinColumns().length);
    assertEquals("", overrides[0].joinTable().name());
}
Also used : AssociationOverrides(javax.persistence.AssociationOverrides) AssociationOverride(javax.persistence.AssociationOverride) Test(org.junit.Test)

Example 4 with AssociationOverride

use of javax.persistence.AssociationOverride in project hibernate-orm by hibernate.

the class Ejb3XmlElementCollectionTest method testMultipleAssociationOverridesJoinColumns.

@Test
public void testMultipleAssociationOverridesJoinColumns() throws Exception {
    reader = getReader(Entity3.class, "field1", "element-collection.orm25.xml");
    assertAnnotationPresent(ElementCollection.class);
    assertAnnotationNotPresent(AssociationOverride.class);
    assertAnnotationPresent(AssociationOverrides.class);
    AssociationOverrides overridesAnno = reader.getAnnotation(AssociationOverrides.class);
    AssociationOverride[] overrides = overridesAnno.value();
    assertEquals(2, overrides.length);
    //First, an association using join table
    assertEquals("association1", overrides[0].name());
    assertEquals(0, overrides[0].joinColumns().length);
    JoinTable joinTableAnno = overrides[0].joinTable();
    assertEquals("catalog1", joinTableAnno.catalog());
    assertEquals("table1", joinTableAnno.name());
    assertEquals("schema1", joinTableAnno.schema());
    //JoinColumns
    JoinColumn[] joinColumns = joinTableAnno.joinColumns();
    assertEquals(2, joinColumns.length);
    assertEquals("", joinColumns[0].name());
    assertEquals("", joinColumns[0].referencedColumnName());
    assertEquals("", joinColumns[0].table());
    assertEquals("", joinColumns[0].columnDefinition());
    assertTrue(joinColumns[0].insertable());
    assertTrue(joinColumns[0].updatable());
    assertTrue(joinColumns[0].nullable());
    assertFalse(joinColumns[0].unique());
    assertEquals("col1", joinColumns[1].name());
    assertEquals("col2", joinColumns[1].referencedColumnName());
    assertEquals("table2", joinColumns[1].table());
    assertEquals("int", joinColumns[1].columnDefinition());
    assertFalse(joinColumns[1].insertable());
    assertFalse(joinColumns[1].updatable());
    assertFalse(joinColumns[1].nullable());
    assertTrue(joinColumns[1].unique());
    //InverseJoinColumns
    JoinColumn[] inverseJoinColumns = joinTableAnno.inverseJoinColumns();
    assertEquals(2, inverseJoinColumns.length);
    assertEquals("", inverseJoinColumns[0].name());
    assertEquals("", inverseJoinColumns[0].referencedColumnName());
    assertEquals("", inverseJoinColumns[0].table());
    assertEquals("", inverseJoinColumns[0].columnDefinition());
    assertTrue(inverseJoinColumns[0].insertable());
    assertTrue(inverseJoinColumns[0].updatable());
    assertTrue(inverseJoinColumns[0].nullable());
    assertFalse(inverseJoinColumns[0].unique());
    assertEquals("col3", inverseJoinColumns[1].name());
    assertEquals("col4", inverseJoinColumns[1].referencedColumnName());
    assertEquals("table3", inverseJoinColumns[1].table());
    assertEquals("int", inverseJoinColumns[1].columnDefinition());
    assertFalse(inverseJoinColumns[1].insertable());
    assertFalse(inverseJoinColumns[1].updatable());
    assertFalse(inverseJoinColumns[1].nullable());
    assertTrue(inverseJoinColumns[1].unique());
    //UniqueConstraints
    UniqueConstraint[] uniqueConstraints = joinTableAnno.uniqueConstraints();
    assertEquals(2, uniqueConstraints.length);
    assertEquals("", uniqueConstraints[0].name());
    assertEquals(1, uniqueConstraints[0].columnNames().length);
    assertEquals("col5", uniqueConstraints[0].columnNames()[0]);
    assertEquals("uq1", uniqueConstraints[1].name());
    assertEquals(2, uniqueConstraints[1].columnNames().length);
    assertEquals("col6", uniqueConstraints[1].columnNames()[0]);
    assertEquals("col7", uniqueConstraints[1].columnNames()[1]);
    //Second, an association using join columns
    assertEquals("association2", overrides[1].name());
    //JoinColumns
    joinColumns = overrides[1].joinColumns();
    assertEquals(2, joinColumns.length);
    assertEquals("", joinColumns[0].name());
    assertEquals("", joinColumns[0].referencedColumnName());
    assertEquals("", joinColumns[0].table());
    assertEquals("", joinColumns[0].columnDefinition());
    assertTrue(joinColumns[0].insertable());
    assertTrue(joinColumns[0].updatable());
    assertTrue(joinColumns[0].nullable());
    assertFalse(joinColumns[0].unique());
    assertEquals("col8", joinColumns[1].name());
    assertEquals("col9", joinColumns[1].referencedColumnName());
    assertEquals("table4", joinColumns[1].table());
    assertEquals("int", joinColumns[1].columnDefinition());
    assertFalse(joinColumns[1].insertable());
    assertFalse(joinColumns[1].updatable());
    assertFalse(joinColumns[1].nullable());
    assertTrue(joinColumns[1].unique());
}
Also used : AssociationOverrides(javax.persistence.AssociationOverrides) AssociationOverride(javax.persistence.AssociationOverride) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) UniqueConstraint(javax.persistence.UniqueConstraint) JoinTable(javax.persistence.JoinTable) Test(org.junit.Test)

Example 5 with AssociationOverride

use of javax.persistence.AssociationOverride in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReader method buildAssociationOverrides.

private List<AssociationOverride> buildAssociationOverrides(Element element, XMLContext.Default defaults) {
    List<Element> subelements = element == null ? null : element.elements("association-override");
    List<AssociationOverride> overrides = new ArrayList<AssociationOverride>();
    if (subelements != null && subelements.size() > 0) {
        for (Element current : subelements) {
            AnnotationDescriptor override = new AnnotationDescriptor(AssociationOverride.class);
            copyStringAttribute(override, current, "name", true);
            override.setValue("joinColumns", getJoinColumns(current, false));
            JoinTable joinTable = buildJoinTable(current, defaults);
            if (joinTable != null) {
                override.setValue("joinTable", joinTable);
            }
            overrides.add((AssociationOverride) AnnotationFactory.create(override));
        }
    }
    return overrides;
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) AssociationOverride(javax.persistence.AssociationOverride) AnnotatedElement(java.lang.reflect.AnnotatedElement) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) JoinTable(javax.persistence.JoinTable)

Aggregations

AssociationOverride (javax.persistence.AssociationOverride)6 AssociationOverrides (javax.persistence.AssociationOverrides)4 JoinTable (javax.persistence.JoinTable)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 Test (org.junit.Test)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 ArrayList (java.util.ArrayList)1 JoinColumn (javax.persistence.JoinColumn)1 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)1 UniqueConstraint (javax.persistence.UniqueConstraint)1 Element (org.dom4j.Element)1