Search in sources :

Example 1 with XMLContext

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

the class JPAXMLOverriddenAnnotationReaderTest method testElementCollectionConverter.

@Test
@TestForIssue(jiraKey = "HHH-11924")
public void testElementCollectionConverter() throws Exception {
    XMLContext context = buildContext("org/hibernate/orm/test/annotations/reflection/orm.xml");
    Field field = Company.class.getDeclaredField("organizations");
    JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader(field, context, bootstrapContext);
    assertNotNull(reader.getAnnotation(ElementCollection.class));
    assertNotNull(reader.getAnnotation(Converts.class));
    assertNotNull(reader.getAnnotation(Converts.class).value());
    assertTrue(reader.getAnnotation(Converts.class).value().length == 1);
    assertEquals(OrganizationConverter.class, reader.getAnnotation(Converts.class).value()[0].converter());
}
Also used : Field(java.lang.reflect.Field) XMLContext(org.hibernate.cfg.annotations.reflection.internal.XMLContext) JPAXMLOverriddenAnnotationReader(org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenAnnotationReader) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with XMLContext

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

the class JPAXMLOverriddenAnnotationReaderTest method testAssociationRelatedAnnotations.

@Test
public void testAssociationRelatedAnnotations() throws Exception {
    XMLContext context = buildContext("org/hibernate/orm/test/annotations/reflection/orm.xml");
    Field field = Administration.class.getDeclaredField("defaultBusTrip");
    JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader(field, context, bootstrapContext);
    assertNotNull(reader.getAnnotation(OneToOne.class));
    assertNull(reader.getAnnotation(JoinColumns.class));
    assertNotNull(reader.getAnnotation(PrimaryKeyJoinColumns.class));
    assertEquals("pk", reader.getAnnotation(PrimaryKeyJoinColumns.class).value()[0].name());
    assertEquals(5, reader.getAnnotation(OneToOne.class).cascade().length);
    assertEquals(FetchType.LAZY, reader.getAnnotation(OneToOne.class).fetch());
    assertEquals("test", reader.getAnnotation(OneToOne.class).mappedBy());
    context = buildContext("org/hibernate/orm/test/annotations/reflection/metadata-complete.xml");
    field = BusTrip.class.getDeclaredField("players");
    reader = new JPAXMLOverriddenAnnotationReader(field, context, bootstrapContext);
    assertNotNull(reader.getAnnotation(OneToMany.class));
    assertNotNull(reader.getAnnotation(JoinColumns.class));
    assertEquals(2, reader.getAnnotation(JoinColumns.class).value().length);
    assertEquals("driver", reader.getAnnotation(JoinColumns.class).value()[0].name());
    assertNotNull(reader.getAnnotation(MapKey.class));
    assertEquals("name", reader.getAnnotation(MapKey.class).name());
    field = BusTrip.class.getDeclaredField("roads");
    reader = new JPAXMLOverriddenAnnotationReader(field, context, bootstrapContext);
    assertNotNull(reader.getAnnotation(ManyToMany.class));
    assertNotNull(reader.getAnnotation(JoinTable.class));
    assertEquals("bus_road", reader.getAnnotation(JoinTable.class).name());
    assertEquals(2, reader.getAnnotation(JoinTable.class).joinColumns().length);
    assertEquals(1, reader.getAnnotation(JoinTable.class).inverseJoinColumns().length);
    assertEquals(2, reader.getAnnotation(JoinTable.class).uniqueConstraints()[0].columnNames().length);
    assertNotNull(reader.getAnnotation(OrderBy.class));
    assertEquals("maxSpeed", reader.getAnnotation(OrderBy.class).value());
}
Also used : Field(java.lang.reflect.Field) XMLContext(org.hibernate.cfg.annotations.reflection.internal.XMLContext) JPAXMLOverriddenAnnotationReader(org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenAnnotationReader) Test(org.junit.Test)

Example 3 with XMLContext

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

the class JPAXMLOverriddenAnnotationReaderTest method testEntityRelatedAnnotations.

@Test
public void testEntityRelatedAnnotations() throws Exception {
    XMLContext context = buildContext("org/hibernate/orm/test/annotations/reflection/orm.xml");
    JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader(Administration.class, context, bootstrapContext);
    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());
    // The default schema is assigned later, when we generate SQL.
    // See DefaultCatalogAndSchemaTest.
    assertEquals("Default schema overridden too soon", "", 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());
    // The default schema is assigned later, when we generate SQL.
    // See DefaultCatalogAndSchemaTest.
    assertEquals("Default schema overridden too soon", "", reader.getAnnotation(TableGenerator.class).schema());
    reader = new JPAXMLOverriddenAnnotationReader(Match.class, context, bootstrapContext);
    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());
    // The default schema is assigned later, when we generate SQL.
    // See DefaultCatalogAndSchemaTest.
    assertEquals("Default catalog overridden too soon", "", reader.getAnnotation(Table.class).catalog());
    assertNotNull("SecondaryTable swallowed", reader.getAnnotation(SecondaryTables.class));
    // The default schema is assigned later, when we generate SQL.
    // See DefaultCatalogAndSchemaTest.
    assertEquals("Default schema not taken into account", "", 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 JPAXMLOverriddenAnnotationReader(Competition.class, context, bootstrapContext);
    assertNotNull(reader.getAnnotation(MappedSuperclass.class));
    reader = new JPAXMLOverriddenAnnotationReader(TennisMatch.class, context, bootstrapContext);
    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 JPAXMLOverriddenAnnotationReader(SocialSecurityPhysicalAccount.class, context, bootstrapContext);
    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 : JPAXMLOverriddenAnnotationReader(org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenAnnotationReader) XMLContext(org.hibernate.cfg.annotations.reflection.internal.XMLContext) Test(org.junit.Test)

Example 4 with XMLContext

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

the class JPAXMLOverriddenAnnotationReaderTest method testVersionRelatedAnnotations.

@Test
public void testVersionRelatedAnnotations() throws Exception {
    XMLContext context = buildContext("org/hibernate/orm/test/annotations/reflection/orm.xml");
    Method method = Administration.class.getDeclaredMethod("getVersion");
    JPAXMLOverriddenAnnotationReader reader = new JPAXMLOverriddenAnnotationReader(method, context, bootstrapContext);
    assertNotNull(reader.getAnnotation(Version.class));
    Field field = Match.class.getDeclaredField("version");
    assertNotNull(reader.getAnnotation(Version.class));
}
Also used : Field(java.lang.reflect.Field) XMLContext(org.hibernate.cfg.annotations.reflection.internal.XMLContext) Method(java.lang.reflect.Method) JPAXMLOverriddenAnnotationReader(org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenAnnotationReader) Test(org.junit.Test)

Example 5 with XMLContext

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

the class XMLContextTest method testAll.

@Test
public void testAll() throws Exception {
    XMLMappingHelper xmlHelper = new XMLMappingHelper();
    final XMLContext context = new XMLContext(bootstrapContext);
    JaxbEntityMappings mappings = xmlHelper.readOrmXmlMappings("org/hibernate/orm/test/annotations/reflection/orm.xml");
    context.addDocument(mappings);
}
Also used : JaxbEntityMappings(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings) XMLMappingHelper(org.hibernate.orm.test.internal.util.xml.XMLMappingHelper) XMLContext(org.hibernate.cfg.annotations.reflection.internal.XMLContext) Test(org.junit.jupiter.api.Test)

Aggregations

XMLContext (org.hibernate.cfg.annotations.reflection.internal.XMLContext)14 JPAXMLOverriddenAnnotationReader (org.hibernate.cfg.annotations.reflection.internal.JPAXMLOverriddenAnnotationReader)11 Test (org.junit.Test)10 Field (java.lang.reflect.Field)6 Method (java.lang.reflect.Method)3 JaxbEntityMappings (org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings)3 XMLMappingHelper (org.hibernate.orm.test.internal.util.xml.XMLMappingHelper)3 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Columns (org.hibernate.annotations.Columns)1 TestForIssue (org.hibernate.testing.TestForIssue)1 Test (org.junit.jupiter.api.Test)1