Search in sources :

Example 6 with JPAOverriddenAnnotationReader

use of org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReaderTest method testTransientAndEmbeddedRelatedAnnotations.

@Test
public void testTransientAndEmbeddedRelatedAnnotations() throws Exception {
    XMLContext context = buildContext("org/hibernate/test/annotations/reflection/orm.xml");
    Field field = Administration.class.getDeclaredField("transientField");
    JPAOverriddenAnnotationReader reader = new JPAOverriddenAnnotationReader(field, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(Transient.class));
    assertNull(reader.getAnnotation(Basic.class));
    field = Match.class.getDeclaredField("playerASSN");
    reader = new JPAOverriddenAnnotationReader(field, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(Embedded.class));
}
Also used : Field(java.lang.reflect.Field) Basic(javax.persistence.Basic) XMLContext(org.hibernate.cfg.annotations.reflection.XMLContext) JPAOverriddenAnnotationReader(org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader) Transient(javax.persistence.Transient) Embedded(javax.persistence.Embedded) Test(org.junit.Test)

Example 7 with JPAOverriddenAnnotationReader

use of org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReaderTest method testEntityRelatedAnnotationsMetadataComplete.

@Test
public void testEntityRelatedAnnotationsMetadataComplete() throws Exception {
    XMLContext context = buildContext("org/hibernate/test/annotations/reflection/metadata-complete.xml");
    JPAOverriddenAnnotationReader reader = new JPAOverriddenAnnotationReader(Administration.class, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(Entity.class));
    assertEquals("Metadata complete should ignore java annotations", "", reader.getAnnotation(Entity.class).name());
    assertNotNull(reader.getAnnotation(Table.class));
    assertEquals("@Table should not be used", "", reader.getAnnotation(Table.class).name());
    assertEquals("Default schema not overriden", "myschema", reader.getAnnotation(Table.class).schema());
    reader = new JPAOverriddenAnnotationReader(Match.class, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(Table.class));
    assertEquals("@Table should not be used", "", reader.getAnnotation(Table.class).name());
    assertEquals("Overriding not taken into account", "myschema", reader.getAnnotation(Table.class).schema());
    assertEquals("Overriding not taken into account", "mycatalog", reader.getAnnotation(Table.class).catalog());
    assertNull("Ignore Java annotation", reader.getAnnotation(SecondaryTable.class));
    assertNull("Ignore Java annotation", reader.getAnnotation(SecondaryTables.class));
    assertNull("Ignore Java annotation", reader.getAnnotation(Inheritance.class));
    assertNull(reader.getAnnotation(NamedQueries.class));
    assertNull(reader.getAnnotation(NamedNativeQueries.class));
    reader = new JPAOverriddenAnnotationReader(TennisMatch.class, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNull(reader.getAnnotation(PrimaryKeyJoinColumn.class));
    assertNull(reader.getAnnotation(PrimaryKeyJoinColumns.class));
    reader = new JPAOverriddenAnnotationReader(Competition.class, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNull(reader.getAnnotation(MappedSuperclass.class));
    reader = new JPAOverriddenAnnotationReader(SocialSecurityMoralAccount.class, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNull(reader.getAnnotation(IdClass.class));
    assertNull(reader.getAnnotation(DiscriminatorValue.class));
    assertNull(reader.getAnnotation(DiscriminatorColumn.class));
    assertNull(reader.getAnnotation(SequenceGenerator.class));
    assertNull(reader.getAnnotation(TableGenerator.class));
}
Also used : Entity(javax.persistence.Entity) JoinTable(javax.persistence.JoinTable) Table(javax.persistence.Table) SecondaryTable(javax.persistence.SecondaryTable) XMLContext(org.hibernate.cfg.annotations.reflection.XMLContext) SequenceGenerator(javax.persistence.SequenceGenerator) Inheritance(javax.persistence.Inheritance) JPAOverriddenAnnotationReader(org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader) NamedQueries(javax.persistence.NamedQueries) TableGenerator(javax.persistence.TableGenerator) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) IdClass(javax.persistence.IdClass) SecondaryTables(javax.persistence.SecondaryTables) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) NamedNativeQueries(javax.persistence.NamedNativeQueries) MappedSuperclass(javax.persistence.MappedSuperclass) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) SecondaryTable(javax.persistence.SecondaryTable) DiscriminatorValue(javax.persistence.DiscriminatorValue) Test(org.junit.Test)

Example 8 with JPAOverriddenAnnotationReader

use of org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader 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 JPAOverriddenAnnotationReader

use of org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader in project hibernate-orm by hibernate.

the class JPAOverriddenAnnotationReaderTest method testIdRelatedAnnotations.

@Test
public void testIdRelatedAnnotations() throws Exception {
    XMLContext context = buildContext("org/hibernate/test/annotations/reflection/orm.xml");
    Method method = Administration.class.getDeclaredMethod("getId");
    JPAOverriddenAnnotationReader reader = new JPAOverriddenAnnotationReader(method, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNull(reader.getAnnotation(Id.class));
    assertNull(reader.getAnnotation(Column.class));
    Field field = Administration.class.getDeclaredField("id");
    reader = new JPAOverriddenAnnotationReader(field, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(Id.class));
    assertNotNull(reader.getAnnotation(GeneratedValue.class));
    assertEquals(GenerationType.SEQUENCE, reader.getAnnotation(GeneratedValue.class).strategy());
    assertEquals("generator", reader.getAnnotation(GeneratedValue.class).generator());
    assertNotNull(reader.getAnnotation(SequenceGenerator.class));
    assertEquals("seq", reader.getAnnotation(SequenceGenerator.class).sequenceName());
    assertNotNull(reader.getAnnotation(Columns.class));
    assertEquals(1, reader.getAnnotation(Columns.class).columns().length);
    assertEquals("fld_id", reader.getAnnotation(Columns.class).columns()[0].name());
    assertNotNull(reader.getAnnotation(Temporal.class));
    assertEquals(TemporalType.DATE, reader.getAnnotation(Temporal.class).value());
    context = buildContext("org/hibernate/test/annotations/reflection/metadata-complete.xml");
    method = Administration.class.getDeclaredMethod("getId");
    reader = new JPAOverriddenAnnotationReader(method, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull("Default access type when not defined in metadata complete should be property", reader.getAnnotation(Id.class));
    field = Administration.class.getDeclaredField("id");
    reader = new JPAOverriddenAnnotationReader(field, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNull("Default access type when not defined in metadata complete should be property", reader.getAnnotation(Id.class));
    method = BusTrip.class.getDeclaredMethod("getId");
    reader = new JPAOverriddenAnnotationReader(method, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNull(reader.getAnnotation(EmbeddedId.class));
    field = BusTrip.class.getDeclaredField("id");
    reader = new JPAOverriddenAnnotationReader(field, context, ClassLoaderAccessTestingImpl.INSTANCE);
    assertNotNull(reader.getAnnotation(EmbeddedId.class));
    assertNotNull(reader.getAnnotation(AttributeOverrides.class));
    assertEquals(1, reader.getAnnotation(AttributeOverrides.class).value().length);
}
Also used : XMLContext(org.hibernate.cfg.annotations.reflection.XMLContext) SequenceGenerator(javax.persistence.SequenceGenerator) JPAOverriddenAnnotationReader(org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader) PrimaryKeyJoinColumns(javax.persistence.PrimaryKeyJoinColumns) Columns(org.hibernate.annotations.Columns) JoinColumns(javax.persistence.JoinColumns) EmbeddedId(javax.persistence.EmbeddedId) Method(java.lang.reflect.Method) GeneratedValue(javax.persistence.GeneratedValue) Field(java.lang.reflect.Field) AttributeOverrides(javax.persistence.AttributeOverrides) Temporal(javax.persistence.Temporal) Column(javax.persistence.Column) DiscriminatorColumn(javax.persistence.DiscriminatorColumn) PrimaryKeyJoinColumn(javax.persistence.PrimaryKeyJoinColumn) Id(javax.persistence.Id) EmbeddedId(javax.persistence.EmbeddedId) Test(org.junit.Test)

Example 10 with JPAOverriddenAnnotationReader

use of org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader in project hibernate-orm by hibernate.

the class Ejb3XmlTestCase method getReader.

protected JPAOverriddenAnnotationReader getReader(Class<?> entityClass, String fieldName, String ormResourceName) throws Exception {
    AnnotatedElement el = getAnnotatedElement(entityClass, fieldName);
    XMLContext xmlContext = getContext(ormResourceName);
    return new JPAOverriddenAnnotationReader(el, xmlContext, ClassLoaderAccessTestingImpl.INSTANCE);
}
Also used : XMLContext(org.hibernate.cfg.annotations.reflection.XMLContext) AnnotatedElement(java.lang.reflect.AnnotatedElement) JPAOverriddenAnnotationReader(org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader)

Aggregations

JPAOverriddenAnnotationReader (org.hibernate.cfg.annotations.reflection.JPAOverriddenAnnotationReader)10 XMLContext (org.hibernate.cfg.annotations.reflection.XMLContext)10 Test (org.junit.Test)9 Field (java.lang.reflect.Field)5 PrimaryKeyJoinColumns (javax.persistence.PrimaryKeyJoinColumns)4 Method (java.lang.reflect.Method)3 DiscriminatorColumn (javax.persistence.DiscriminatorColumn)3 JoinTable (javax.persistence.JoinTable)3 MappedSuperclass (javax.persistence.MappedSuperclass)3 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)3 SequenceGenerator (javax.persistence.SequenceGenerator)3 AttributeOverrides (javax.persistence.AttributeOverrides)2 Basic (javax.persistence.Basic)2 Entity (javax.persistence.Entity)2 IdClass (javax.persistence.IdClass)2 Inheritance (javax.persistence.Inheritance)2 JoinColumns (javax.persistence.JoinColumns)2 NamedNativeQueries (javax.persistence.NamedNativeQueries)2 NamedQueries (javax.persistence.NamedQueries)2 SecondaryTable (javax.persistence.SecondaryTable)2