Search in sources :

Example 6 with JoinColumns

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

the class Ejb3XmlManyToOneTest method testSingleJoinColumn.

/**
 * When there's a single join column, we still wrap it with a JoinColumns
 * annotation.
 */
@Test
public void testSingleJoinColumn() throws Exception {
    reader = getReader(Entity1.class, "field1", "many-to-one.orm2.xml");
    assertAnnotationPresent(ManyToOne.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationPresent(JoinColumns.class);
    assertAnnotationNotPresent(JoinTable.class);
    JoinColumns joinColumnsAnno = reader.getAnnotation(JoinColumns.class);
    JoinColumn[] joinColumns = joinColumnsAnno.value();
    assertEquals(1, joinColumns.length);
    assertEquals("col1", joinColumns[0].name());
    assertEquals("col2", joinColumns[0].referencedColumnName());
    assertEquals("table1", joinColumns[0].table());
}
Also used : JoinColumn(javax.persistence.JoinColumn) JoinColumns(javax.persistence.JoinColumns) Test(org.junit.Test)

Example 7 with JoinColumns

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

the class Ejb3XmlOneToOneTest method testMultipleJoinColumns.

@Test
public void testMultipleJoinColumns() throws Exception {
    reader = getReader(Entity1.class, "field1", "one-to-one.orm5.xml");
    assertAnnotationPresent(OneToOne.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumn.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumns.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationPresent(JoinColumns.class);
    assertAnnotationNotPresent(JoinTable.class);
    JoinColumns joinColumnsAnno = reader.getAnnotation(JoinColumns.class);
    JoinColumn[] joinColumns = joinColumnsAnno.value();
    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("table1", 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 : JoinColumn(javax.persistence.JoinColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) Test(org.junit.Test)

Example 8 with JoinColumns

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

the class Ejb3XmlOneToOneTest method testSingleJoinColumn.

/**
 * When there's a single join column, we still wrap it with a JoinColumns
 * annotation.
 */
@Test
public void testSingleJoinColumn() throws Exception {
    reader = getReader(Entity1.class, "field1", "one-to-one.orm4.xml");
    assertAnnotationPresent(OneToOne.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumn.class);
    assertAnnotationNotPresent(PrimaryKeyJoinColumns.class);
    assertAnnotationPresent(JoinColumns.class);
    assertAnnotationNotPresent(JoinColumn.class);
    assertAnnotationNotPresent(JoinTable.class);
    JoinColumns joinColumnsAnno = reader.getAnnotation(JoinColumns.class);
    JoinColumn[] joinColumns = joinColumnsAnno.value();
    assertEquals(1, joinColumns.length);
    assertEquals("col1", joinColumns[0].name());
    assertEquals("col2", joinColumns[0].referencedColumnName());
    assertEquals("table1", joinColumns[0].table());
}
Also used : JoinColumn(javax.persistence.JoinColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) Test(org.junit.Test)

Example 9 with JoinColumns

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

the class JPAOverriddenAnnotationReader method overridesDefaultsInJoinTable.

private JoinTable overridesDefaultsInJoinTable(Annotation annotation, XMLContext.Default defaults) {
    // no element but might have some default or some annotation
    boolean defaultToJoinTable = !(isPhysicalAnnotationPresent(JoinColumn.class) || isPhysicalAnnotationPresent(JoinColumns.class));
    final Class<? extends Annotation> annotationClass = annotation.annotationType();
    defaultToJoinTable = defaultToJoinTable && ((annotationClass == ManyToMany.class && StringHelper.isEmpty(((ManyToMany) annotation).mappedBy())) || (annotationClass == OneToMany.class && StringHelper.isEmpty(((OneToMany) annotation).mappedBy())) || (annotationClass == ElementCollection.class));
    final Class<JoinTable> annotationType = JoinTable.class;
    if (defaultToJoinTable && (StringHelper.isNotEmpty(defaults.getCatalog()) || StringHelper.isNotEmpty(defaults.getSchema()))) {
        AnnotationDescriptor ad = new AnnotationDescriptor(annotationType);
        if (defaults.canUseJavaAnnotations()) {
            JoinTable table = getPhysicalAnnotation(annotationType);
            if (table != null) {
                ad.setValue("name", table.name());
                ad.setValue("schema", table.schema());
                ad.setValue("catalog", table.catalog());
                ad.setValue("uniqueConstraints", table.uniqueConstraints());
                ad.setValue("joinColumns", table.joinColumns());
                ad.setValue("inverseJoinColumns", table.inverseJoinColumns());
            }
        }
        if (StringHelper.isEmpty((String) ad.valueOf("schema")) && StringHelper.isNotEmpty(defaults.getSchema())) {
            ad.setValue("schema", defaults.getSchema());
        }
        if (StringHelper.isEmpty((String) ad.valueOf("catalog")) && StringHelper.isNotEmpty(defaults.getCatalog())) {
            ad.setValue("catalog", defaults.getCatalog());
        }
        return AnnotationFactory.create(ad);
    } else if (defaults.canUseJavaAnnotations()) {
        return getPhysicalAnnotation(annotationType);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) ManyToMany(javax.persistence.ManyToMany) ElementCollection(javax.persistence.ElementCollection) OneToMany(javax.persistence.OneToMany) JoinTable(javax.persistence.JoinTable)

Example 10 with JoinColumns

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

the class AnnotationBinder method addProperty.

private static int addProperty(PropertyContainer propertyContainer, XProperty property, List<PropertyData> inFlightPropertyDataList, MetadataBuildingContext context) {
    // and if so, skip it..
    for (PropertyData propertyData : inFlightPropertyDataList) {
        if (propertyData.getPropertyName().equals(property.getName())) {
            // EARLY EXIT!!!
            return 0;
        }
    }
    final XClass declaringClass = propertyContainer.getDeclaringClass();
    final XClass entity = propertyContainer.getEntityAtStake();
    int idPropertyCounter = 0;
    PropertyData propertyAnnotatedElement = new PropertyInferredData(declaringClass, property, propertyContainer.getClassLevelAccessType().getType(), context.getBootstrapContext().getReflectionManager());
    /*
		 * put element annotated by @Id in front
		 * since it has to be parsed before any association by Hibernate
		 */
    final XAnnotatedElement element = propertyAnnotatedElement.getProperty();
    if (element.isAnnotationPresent(Id.class) || element.isAnnotationPresent(EmbeddedId.class)) {
        inFlightPropertyDataList.add(0, propertyAnnotatedElement);
        /**
         * The property must be put in hibernate.properties as it's a system wide property. Fixable?
         * TODO support true/false/default on the property instead of present / not present
         * TODO is @Column mandatory?
         * TODO add method support
         */
        if (context.getBuildingOptions().isSpecjProprietarySyntaxEnabled()) {
            if (element.isAnnotationPresent(Id.class) && element.isAnnotationPresent(Column.class)) {
                String columnName = element.getAnnotation(Column.class).name();
                for (XProperty prop : declaringClass.getDeclaredProperties(AccessType.FIELD.getType())) {
                    if (!prop.isAnnotationPresent(MapsId.class)) {
                        /**
                         * The detection of a configured individual JoinColumn differs between Annotation
                         * and XML configuration processing.
                         */
                        boolean isRequiredAnnotationPresent = false;
                        JoinColumns groupAnnotation = prop.getAnnotation(JoinColumns.class);
                        if ((prop.isAnnotationPresent(JoinColumn.class) && prop.getAnnotation(JoinColumn.class).name().equals(columnName))) {
                            isRequiredAnnotationPresent = true;
                        } else if (prop.isAnnotationPresent(JoinColumns.class)) {
                            for (JoinColumn columnAnnotation : groupAnnotation.value()) {
                                if (columnName.equals(columnAnnotation.name())) {
                                    isRequiredAnnotationPresent = true;
                                    break;
                                }
                            }
                        }
                        if (isRequiredAnnotationPresent) {
                            // create a PropertyData fpr the specJ property holding the mapping
                            PropertyData specJPropertyData = new PropertyInferredData(declaringClass, // same dec
                            prop, // the actual @XToOne property
                            propertyContainer.getClassLevelAccessType().getType(), // TODO we should get the right accessor but the same as id would do
                            context.getBootstrapContext().getReflectionManager());
                            context.getMetadataCollector().addPropertyAnnotatedWithMapsIdSpecj(entity, specJPropertyData, element.toString());
                        }
                    }
                }
            }
        }
        if (element.isAnnotationPresent(ManyToOne.class) || element.isAnnotationPresent(OneToOne.class)) {
            context.getMetadataCollector().addToOneAndIdProperty(entity, propertyAnnotatedElement);
        }
        idPropertyCounter++;
    } else {
        inFlightPropertyDataList.add(propertyAnnotatedElement);
    }
    if (element.isAnnotationPresent(MapsId.class)) {
        context.getMetadataCollector().addPropertyAnnotatedWithMapsId(entity, propertyAnnotatedElement);
    }
    return idPropertyCounter;
}
Also used : MapsId(javax.persistence.MapsId) XProperty(org.hibernate.annotations.common.reflection.XProperty) EmbeddedId(javax.persistence.EmbeddedId) MapKeyJoinColumns(javax.persistence.MapKeyJoinColumns) JoinColumns(javax.persistence.JoinColumns) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) XAnnotatedElement(org.hibernate.annotations.common.reflection.XAnnotatedElement) XClass(org.hibernate.annotations.common.reflection.XClass) UniqueConstraint(javax.persistence.UniqueConstraint) Constraint(org.hibernate.mapping.Constraint) ManyToOne(javax.persistence.ManyToOne) OneToOne(javax.persistence.OneToOne) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) JoinColumn(javax.persistence.JoinColumn) MapKeyColumn(javax.persistence.MapKeyColumn) OrderColumn(javax.persistence.OrderColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) MapKeyJoinColumn(javax.persistence.MapKeyJoinColumn) Column(javax.persistence.Column) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) JoinColumn(javax.persistence.JoinColumn) MapsId(javax.persistence.MapsId) NaturalId(org.hibernate.annotations.NaturalId) EmbeddedId(javax.persistence.EmbeddedId) Id(javax.persistence.Id) CollectionId(org.hibernate.annotations.CollectionId)

Aggregations

JoinColumn (javax.persistence.JoinColumn)12 JoinColumns (javax.persistence.JoinColumns)12 MapKeyJoinColumn (javax.persistence.MapKeyJoinColumn)6 MapKeyJoinColumns (javax.persistence.MapKeyJoinColumns)6 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)6 PrimaryKeyJoinColumns (javax.persistence.PrimaryKeyJoinColumns)6 Test (org.junit.Test)6 Column (javax.persistence.Column)4 ManyToOne (javax.persistence.ManyToOne)4 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)3 EmbeddedId (javax.persistence.EmbeddedId)3 Id (javax.persistence.Id)3 MapKeyColumn (javax.persistence.MapKeyColumn)3 MapsId (javax.persistence.MapsId)3 OneToMany (javax.persistence.OneToMany)3 OneToOne (javax.persistence.OneToOne)3 OrderColumn (javax.persistence.OrderColumn)3 AnnotationException (org.hibernate.AnnotationException)3 CollectionId (org.hibernate.annotations.CollectionId)3 NaturalId (org.hibernate.annotations.NaturalId)3