Search in sources :

Example 6 with PrimaryKeyJoinColumn

use of javax.persistence.PrimaryKeyJoinColumn in project cloudstack by apache.

the class SqlGenerator method handleDaoAttributes.

protected void handleDaoAttributes(Class<?> clazz) {
    Attribute attr;
    Class<?> current = clazz;
    while (current != null && current.getAnnotation(Entity.class) != null) {
        DiscriminatorColumn column = current.getAnnotation(DiscriminatorColumn.class);
        if (column != null) {
            String columnName = column.name();
            attr = findAttribute(columnName);
            if (attr != null) {
                attr.setTrue(Attribute.Flag.DaoGenerated);
                attr.setTrue(Attribute.Flag.Insertable);
                attr.setTrue(Attribute.Flag.Updatable);
                attr.setFalse(Attribute.Flag.Nullable);
                attr.setTrue(Attribute.Flag.DC);
            } else {
                attr = new Attribute(DbUtil.getTableName(current), column.name());
                attr.setFalse(Flag.Selectable);
                attr.setTrue(Flag.Insertable);
                attr.setTrue(Flag.DaoGenerated);
                attr.setTrue(Flag.DC);
                _attributes.add(attr);
            }
            if (column.discriminatorType() == DiscriminatorType.CHAR) {
                attr.setTrue(Attribute.Flag.CharDT);
            } else if (column.discriminatorType() == DiscriminatorType.STRING) {
                attr.setTrue(Attribute.Flag.StringDT);
            } else if (column.discriminatorType() == DiscriminatorType.INTEGER) {
                attr.setTrue(Attribute.Flag.IntegerDT);
            }
        }
        PrimaryKeyJoinColumn[] pkjcs = DbUtil.getPrimaryKeyJoinColumns(current);
        if (pkjcs != null) {
            for (PrimaryKeyJoinColumn pkjc : pkjcs) {
                String tableName = DbUtil.getTableName(current);
                attr = findAttribute(pkjc.name());
                if (attr == null || !tableName.equals(attr.table)) {
                    Attribute id = new Attribute(DbUtil.getTableName(current), pkjc.name());
                    if (pkjc.referencedColumnName().length() > 0) {
                        attr = findAttribute(pkjc.referencedColumnName());
                        assert (attr != null) : "Couldn't find referenced column name " + pkjc.referencedColumnName();
                    }
                    id.field = attr.field;
                    id.setTrue(Flag.Id);
                    id.setTrue(Flag.Insertable);
                    id.setFalse(Flag.Updatable);
                    id.setFalse(Flag.Nullable);
                    id.setFalse(Flag.Selectable);
                    _attributes.add(id);
                    List<Attribute> attrs = _ids.get(id.table);
                    attrs.add(id);
                }
            }
        }
        current = current.getSuperclass();
    }
    attr = findAttribute(GenericDao.CREATED_COLUMN);
    if (attr != null && attr.field.getType() == Date.class) {
        attr.setTrue(Attribute.Flag.DaoGenerated);
        attr.setTrue(Attribute.Flag.Insertable);
        attr.setFalse(Attribute.Flag.Updatable);
        attr.setFalse(Attribute.Flag.Date);
        attr.setFalse(Attribute.Flag.Time);
        attr.setTrue(Attribute.Flag.TimeStamp);
        attr.setFalse(Attribute.Flag.Nullable);
        attr.setTrue(Attribute.Flag.Created);
    }
    attr = findAttribute(GenericDao.XID_COLUMN);
    if (attr != null && attr.field.getType() == String.class) {
        attr.setTrue(Attribute.Flag.DaoGenerated);
        attr.setTrue(Attribute.Flag.Insertable);
        attr.setFalse(Attribute.Flag.Updatable);
        attr.setFalse(Attribute.Flag.TimeStamp);
        attr.setFalse(Attribute.Flag.Time);
        attr.setFalse(Attribute.Flag.Date);
        attr.setFalse(Attribute.Flag.Nullable);
        attr.setFalse(Attribute.Flag.Removed);
    }
}
Also used : PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) Date(java.util.Date)

Example 7 with PrimaryKeyJoinColumn

use of javax.persistence.PrimaryKeyJoinColumn in project cloudstack by apache.

the class SqlGenerator method addPrimaryKeyJoinColumns.

protected static void addPrimaryKeyJoinColumns(StringBuilder sql, String fromTable, String toTable, String joinType, PrimaryKeyJoinColumn[] pkjcs) {
    if ("right".equalsIgnoreCase(joinType)) {
        sql.append(" RIGHT JOIN ").append(toTable).append(" ON ");
    } else if ("left".equalsIgnoreCase(joinType)) {
        sql.append(" LEFT JOIN ").append(toTable).append(" ON ");
    } else {
        sql.append(" INNER JOIN ").append(toTable).append(" ON ");
    }
    for (PrimaryKeyJoinColumn pkjc : pkjcs) {
        sql.append(fromTable).append(".").append(pkjc.name());
        String refColumn = DbUtil.getReferenceColumn(pkjc);
        sql.append("=").append(toTable).append(".").append(refColumn).append(" ");
    }
}
Also used : PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn)

Example 8 with PrimaryKeyJoinColumn

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

the class JPAOverriddenAnnotationReaderTest method testEntityRelatedAnnotations.

@Test
public void testEntityRelatedAnnotations() throws Exception {
    XMLContext context = buildContext("org/hibernate/test/annotations/reflection/orm.xml");
    JPAOverriddenAnnotationReader reader = new JPAOverriddenAnnotationReader(Administration.class, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(Entity.class));
    assertEquals("Default value in xml entity should not override @Entity.name", "JavaAdministration", reader.getAnnotation(Entity.class).name());
    assertNotNull(reader.getAnnotation(Table.class));
    assertEquals("@Table not overridden", "tbl_admin", reader.getAnnotation(Table.class).name());
    assertEquals("Default schema not overridden", "myschema", reader.getAnnotation(Table.class).schema());
    assertEquals("Proper @Table.uniqueConstraints", 2, reader.getAnnotation(Table.class).uniqueConstraints()[0].columnNames().length);
    String columnName = reader.getAnnotation(Table.class).uniqueConstraints()[0].columnNames()[0];
    assertTrue("Proper @Table.uniqueConstraints", "firstname".equals(columnName) || "lastname".equals(columnName));
    assertNull("Both Java and XML used", reader.getAnnotation(SecondaryTable.class));
    assertNotNull("XML does not work", reader.getAnnotation(SecondaryTables.class));
    SecondaryTable[] tables = reader.getAnnotation(SecondaryTables.class).value();
    assertEquals(1, tables.length);
    assertEquals("admin2", tables[0].name());
    assertEquals("unique constraints ignored", 1, tables[0].uniqueConstraints().length);
    assertEquals("pk join column ignored", 1, tables[0].pkJoinColumns().length);
    assertEquals("pk join column ignored", "admin_id", tables[0].pkJoinColumns()[0].name());
    assertNotNull("Sequence Overriding not working", reader.getAnnotation(SequenceGenerator.class));
    assertEquals("wrong sequence name", "seqhilo", reader.getAnnotation(SequenceGenerator.class).sequenceName());
    assertEquals("default fails", 50, reader.getAnnotation(SequenceGenerator.class).allocationSize());
    assertNotNull("TableOverriding not working", reader.getAnnotation(TableGenerator.class));
    assertEquals("wrong tble name", "tablehilo", reader.getAnnotation(TableGenerator.class).table());
    assertEquals("no schema overriding", "myschema", reader.getAnnotation(TableGenerator.class).schema());
    reader = new JPAOverriddenAnnotationReader(Match.class, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(Table.class));
    assertEquals("Java annotation not taken into account", "matchtable", reader.getAnnotation(Table.class).name());
    assertEquals("Java annotation not taken into account", "matchschema", reader.getAnnotation(Table.class).schema());
    assertEquals("Overriding not taken into account", "mycatalog", reader.getAnnotation(Table.class).catalog());
    assertNotNull("SecondaryTable swallowed", reader.getAnnotation(SecondaryTables.class));
    assertEquals("Default schema not taken into account", "myschema", reader.getAnnotation(SecondaryTables.class).value()[0].schema());
    assertNotNull(reader.getAnnotation(Inheritance.class));
    assertEquals("inheritance strategy not overriden", InheritanceType.JOINED, reader.getAnnotation(Inheritance.class).strategy());
    assertNotNull("NamedQuery not overriden", reader.getAnnotation(NamedQueries.class));
    assertEquals("No deduplication", 3, reader.getAnnotation(NamedQueries.class).value().length);
    assertEquals("deduplication kept the Java version", 1, reader.getAnnotation(NamedQueries.class).value()[1].hints().length);
    assertEquals("org.hibernate.timeout", reader.getAnnotation(NamedQueries.class).value()[1].hints()[0].name());
    assertNotNull("NamedNativeQuery not overriden", reader.getAnnotation(NamedNativeQueries.class));
    assertEquals("No deduplication", 3, reader.getAnnotation(NamedNativeQueries.class).value().length);
    assertEquals("deduplication kept the Java version", 1, reader.getAnnotation(NamedNativeQueries.class).value()[1].hints().length);
    assertEquals("org.hibernate.timeout", reader.getAnnotation(NamedNativeQueries.class).value()[1].hints()[0].name());
    assertNotNull(reader.getAnnotation(SqlResultSetMappings.class));
    assertEquals("competitor1Point", reader.getAnnotation(SqlResultSetMappings.class).value()[0].columns()[0].name());
    assertEquals("competitor1Point", reader.getAnnotation(SqlResultSetMappings.class).value()[0].entities()[0].fields()[0].column());
    assertNotNull(reader.getAnnotation(ExcludeSuperclassListeners.class));
    assertNotNull(reader.getAnnotation(ExcludeDefaultListeners.class));
    reader = new JPAOverriddenAnnotationReader(Competition.class, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(MappedSuperclass.class));
    reader = new JPAOverriddenAnnotationReader(TennisMatch.class, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNull("Mutualize PKJC into PKJCs", reader.getAnnotation(PrimaryKeyJoinColumn.class));
    assertNotNull(reader.getAnnotation(PrimaryKeyJoinColumns.class));
    assertEquals("PrimaryKeyJoinColumn overrden", "id", reader.getAnnotation(PrimaryKeyJoinColumns.class).value()[0].name());
    assertNotNull(reader.getAnnotation(AttributeOverrides.class));
    assertEquals("Wrong deduplication", 3, reader.getAnnotation(AttributeOverrides.class).value().length);
    assertEquals("Wrong priority (XML vs java annotations)", "fld_net", reader.getAnnotation(AttributeOverrides.class).value()[0].column().name());
    assertEquals("Column mapping", 2, reader.getAnnotation(AttributeOverrides.class).value()[1].column().scale());
    assertEquals("Column mapping", true, reader.getAnnotation(AttributeOverrides.class).value()[1].column().unique());
    assertNotNull(reader.getAnnotation(AssociationOverrides.class));
    assertEquals("no XML processing", 1, reader.getAnnotation(AssociationOverrides.class).value().length);
    assertEquals("wrong xml processing", "id", reader.getAnnotation(AssociationOverrides.class).value()[0].joinColumns()[0].referencedColumnName());
    reader = new JPAOverriddenAnnotationReader(SocialSecurityPhysicalAccount.class, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(IdClass.class));
    assertEquals("id-class not used", SocialSecurityNumber.class, reader.getAnnotation(IdClass.class).value());
    assertEquals("discriminator-value not used", "Physical", reader.getAnnotation(DiscriminatorValue.class).value());
    assertNotNull("discriminator-column not used", reader.getAnnotation(DiscriminatorColumn.class));
    assertEquals("discriminator-column.name default value broken", "DTYPE", reader.getAnnotation(DiscriminatorColumn.class).name());
    assertEquals("discriminator-column.length broken", 34, reader.getAnnotation(DiscriminatorColumn.class).length());
}
Also used : Entity(javax.persistence.Entity) Inheritance(javax.persistence.Inheritance) JPAOverriddenAnnotationReader(org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader) SqlResultSetMappings(javax.persistence.SqlResultSetMappings) NamedQueries(javax.persistence.NamedQueries) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) IdClass(javax.persistence.IdClass) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) ExcludeSuperclassListeners(javax.persistence.ExcludeSuperclassListeners) JoinTable(javax.persistence.JoinTable) Table(javax.persistence.Table) SecondaryTable(javax.persistence.SecondaryTable) XMLContext(org.hibernate.cfg.annotations.reflection.XMLContext) SequenceGenerator(javax.persistence.SequenceGenerator) TableGenerator(javax.persistence.TableGenerator) AttributeOverrides(javax.persistence.AttributeOverrides) AssociationOverrides(javax.persistence.AssociationOverrides) SecondaryTables(javax.persistence.SecondaryTables) NamedNativeQueries(javax.persistence.NamedNativeQueries) ExcludeDefaultListeners(javax.persistence.ExcludeDefaultListeners) MappedSuperclass(javax.persistence.MappedSuperclass) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) SecondaryTable(javax.persistence.SecondaryTable) Test(org.junit.Test)

Example 9 with PrimaryKeyJoinColumn

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

the class Ejb3XmlOneToOneTest method testSinglePrimaryKeyJoinColumn.

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

Example 10 with PrimaryKeyJoinColumn

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

the class JPAOverriddenAnnotationReader method getPrimaryKeyJoinColumns.

/**
	 * @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 association mapping) merging with
	 * annotations is never allowed.
	 */
private PrimaryKeyJoinColumns getPrimaryKeyJoinColumns(Element element, XMLContext.Default defaults, boolean mergeWithAnnotations) {
    PrimaryKeyJoinColumn[] columns = buildPrimaryKeyJoinColumns(element);
    if (mergeWithAnnotations) {
        if (columns.length == 0 && defaults.canUseJavaAnnotations()) {
            PrimaryKeyJoinColumn annotation = getPhysicalAnnotation(PrimaryKeyJoinColumn.class);
            if (annotation != null) {
                columns = new PrimaryKeyJoinColumn[] { annotation };
            } else {
                PrimaryKeyJoinColumns annotations = getPhysicalAnnotation(PrimaryKeyJoinColumns.class);
                columns = annotations != null ? annotations.value() : columns;
            }
        }
    }
    if (columns.length > 0) {
        AnnotationDescriptor ad = new AnnotationDescriptor(PrimaryKeyJoinColumns.class);
        ad.setValue("value", columns);
        return AnnotationFactory.create(ad);
    } else {
        return null;
    }
}
Also used : AnnotationDescriptor(org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns)

Aggregations

PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)17 PrimaryKeyJoinColumns (javax.persistence.PrimaryKeyJoinColumns)8 ArrayList (java.util.ArrayList)4 Entity (javax.persistence.Entity)4 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)3 SecondaryTable (javax.persistence.SecondaryTable)3 Test (org.junit.Test)3 Date (java.util.Date)2 JoinColumn (javax.persistence.JoinColumn)2 JoinTable (javax.persistence.JoinTable)2 MappedSuperclass (javax.persistence.MappedSuperclass)2 Table (javax.persistence.Table)2 UniqueConstraint (javax.persistence.UniqueConstraint)2 AnnotationDescriptor (org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 AssociationOverrides (javax.persistence.AssociationOverrides)1 AttributeOverrides (javax.persistence.AttributeOverrides)1