Search in sources :

Example 1 with JaxbEntityMappings

use of org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings in project hibernate-orm by hibernate.

the class MappingBinder method doBind.

@Override
protected Binding<?> doBind(XMLEventReader staxEventReader, StartElement rootElementStartEvent, Origin origin) {
    final String rootElementLocalName = rootElementStartEvent.getName().getLocalPart();
    if ("hibernate-mapping".equals(rootElementLocalName)) {
        if (log.isDebugEnabled()) {
            log.debugf("Performing JAXB binding of hbm.xml document : %s", origin.toString());
        }
        XMLEventReader hbmReader = new HbmEventReader(staxEventReader, xmlEventFactory);
        JaxbHbmHibernateMapping hbmBindings = jaxb(hbmReader, MappingXsdSupport.INSTANCE.hbmXsd().getSchema(), hbmJaxbContext(), origin);
        return new Binding<>(hbmBindings, origin);
    } else {
        try {
            log.debugf("Performing JAXB binding of orm.xml document : %s", origin.toString());
            XMLEventReader reader = new JpaOrmXmlEventReader(staxEventReader, xmlEventFactory);
            JaxbEntityMappings bindingRoot = jaxb(reader, MappingXsdSupport.INSTANCE.latestJpaDescriptor().getSchema(), entityMappingsJaxbContext(), origin);
            return new Binding<>(bindingRoot, origin);
        } catch (JpaOrmXmlEventReader.BadVersionException e) {
            throw new UnsupportedOrmXsdVersionException(e.getRequestedVersion(), origin);
        }
    }
}
Also used : Binding(org.hibernate.boot.jaxb.spi.Binding) JaxbEntityMappings(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings) HbmEventReader(org.hibernate.boot.jaxb.internal.stax.HbmEventReader) XMLEventReader(javax.xml.stream.XMLEventReader) JaxbHbmHibernateMapping(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping) JpaOrmXmlEventReader(org.hibernate.boot.jaxb.internal.stax.JpaOrmXmlEventReader) UnsupportedOrmXsdVersionException(org.hibernate.boot.UnsupportedOrmXsdVersionException)

Example 2 with JaxbEntityMappings

use of org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings 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)

Example 3 with JaxbEntityMappings

use of org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings in project hibernate-orm by hibernate.

the class XMLMappingHelper method readOrmXmlMappings.

public JaxbEntityMappings readOrmXmlMappings(InputStream is, String name) {
    try {
        Assert.assertNotNull("Resource not found: " + name, is);
        Binding<?> binding = binder.bind(is, new Origin(SourceType.JAR, name));
        return (JaxbEntityMappings) binding.getRoot();
    } catch (RuntimeException e) {
        throw new IllegalStateException("Could not parse orm.xml mapping '" + name + "': " + e.getMessage(), e);
    }
}
Also used : Origin(org.hibernate.boot.jaxb.Origin) JaxbEntityMappings(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings)

Example 4 with JaxbEntityMappings

use of org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings in project hibernate-orm by hibernate.

the class JPAXMLOverriddenMetadataProvider method getDefaults.

@Override
public Map<Object, Object> getDefaults() {
    if (!xmlMappingEnabled) {
        return Collections.emptyMap();
    } else {
        if (defaults == null) {
            defaults = new HashMap<>();
            XMLContext.Default xmlDefaults = xmlContext.getDefaultWithGlobalCatalogAndSchema();
            defaults.put("schema", xmlDefaults.getSchema());
            defaults.put("catalog", xmlDefaults.getCatalog());
            defaults.put("delimited-identifier", xmlDefaults.getDelimitedIdentifier());
            defaults.put("cascade-persist", xmlDefaults.getCascadePersist());
            List<Class> entityListeners = new ArrayList<Class>();
            for (String className : xmlContext.getDefaultEntityListeners()) {
                try {
                    entityListeners.add(classLoaderAccess.classForName(className));
                } catch (ClassLoadingException e) {
                    throw new IllegalStateException("Default entity listener class not found: " + className);
                }
            }
            defaults.put(EntityListeners.class, entityListeners);
            for (JaxbEntityMappings entityMappings : xmlContext.getAllDocuments()) {
                List<JaxbSequenceGenerator> jaxbSequenceGenerators = entityMappings.getSequenceGenerator();
                List<SequenceGenerator> sequenceGenerators = (List<SequenceGenerator>) defaults.get(SequenceGenerator.class);
                if (sequenceGenerators == null) {
                    sequenceGenerators = new ArrayList<>();
                    defaults.put(SequenceGenerator.class, sequenceGenerators);
                }
                for (JaxbSequenceGenerator element : jaxbSequenceGenerators) {
                    sequenceGenerators.add(JPAXMLOverriddenAnnotationReader.buildSequenceGeneratorAnnotation(element));
                }
                List<JaxbTableGenerator> jaxbTableGenerators = entityMappings.getTableGenerator();
                List<TableGenerator> tableGenerators = (List<TableGenerator>) defaults.get(TableGenerator.class);
                if (tableGenerators == null) {
                    tableGenerators = new ArrayList<>();
                    defaults.put(TableGenerator.class, tableGenerators);
                }
                for (JaxbTableGenerator element : jaxbTableGenerators) {
                    tableGenerators.add(JPAXMLOverriddenAnnotationReader.buildTableGeneratorAnnotation(element, xmlDefaults));
                }
                List<NamedQuery> namedQueries = (List<NamedQuery>) defaults.get(NamedQuery.class);
                if (namedQueries == null) {
                    namedQueries = new ArrayList<>();
                    defaults.put(NamedQuery.class, namedQueries);
                }
                List<NamedQuery> currentNamedQueries = JPAXMLOverriddenAnnotationReader.buildNamedQueries(entityMappings.getNamedQuery(), xmlDefaults, classLoaderAccess);
                namedQueries.addAll(currentNamedQueries);
                List<NamedNativeQuery> namedNativeQueries = (List<NamedNativeQuery>) defaults.get(NamedNativeQuery.class);
                if (namedNativeQueries == null) {
                    namedNativeQueries = new ArrayList<>();
                    defaults.put(NamedNativeQuery.class, namedNativeQueries);
                }
                List<NamedNativeQuery> currentNamedNativeQueries = JPAXMLOverriddenAnnotationReader.buildNamedNativeQueries(entityMappings.getNamedNativeQuery(), xmlDefaults, classLoaderAccess);
                namedNativeQueries.addAll(currentNamedNativeQueries);
                List<SqlResultSetMapping> sqlResultSetMappings = (List<SqlResultSetMapping>) defaults.get(SqlResultSetMapping.class);
                if (sqlResultSetMappings == null) {
                    sqlResultSetMappings = new ArrayList<>();
                    defaults.put(SqlResultSetMapping.class, sqlResultSetMappings);
                }
                List<SqlResultSetMapping> currentSqlResultSetMappings = JPAXMLOverriddenAnnotationReader.buildSqlResultsetMappings(entityMappings.getSqlResultSetMapping(), xmlDefaults, classLoaderAccess);
                sqlResultSetMappings.addAll(currentSqlResultSetMappings);
                List<NamedStoredProcedureQuery> namedStoredProcedureQueries = (List<NamedStoredProcedureQuery>) defaults.get(NamedStoredProcedureQuery.class);
                if (namedStoredProcedureQueries == null) {
                    namedStoredProcedureQueries = new ArrayList<>();
                    defaults.put(NamedStoredProcedureQuery.class, namedStoredProcedureQueries);
                }
                List<NamedStoredProcedureQuery> currentNamedStoredProcedureQueries = JPAXMLOverriddenAnnotationReader.buildNamedStoreProcedureQueries(entityMappings.getNamedStoredProcedureQuery(), xmlDefaults, classLoaderAccess);
                namedStoredProcedureQueries.addAll(currentNamedStoredProcedureQueries);
            }
        }
        return defaults;
    }
}
Also used : JaxbSequenceGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbSequenceGenerator) NamedNativeQuery(jakarta.persistence.NamedNativeQuery) ArrayList(java.util.ArrayList) JaxbEntityMappings(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings) JaxbTableGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbTableGenerator) ArrayList(java.util.ArrayList) List(java.util.List) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) JaxbSequenceGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbSequenceGenerator) SequenceGenerator(jakarta.persistence.SequenceGenerator) JaxbTableGenerator(org.hibernate.boot.jaxb.mapping.spi.JaxbTableGenerator) TableGenerator(jakarta.persistence.TableGenerator) NamedQuery(jakarta.persistence.NamedQuery) SqlResultSetMapping(jakarta.persistence.SqlResultSetMapping) NamedStoredProcedureQuery(jakarta.persistence.NamedStoredProcedureQuery)

Example 5 with JaxbEntityMappings

use of org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings in project hibernate-orm by hibernate.

the class JakartaXmlSmokeTests method testLoadingOrmXml.

@Test
public void testLoadingOrmXml(ServiceRegistryScope scope) {
    final ClassLoaderService cls = scope.getRegistry().getService(ClassLoaderService.class);
    final MappingBinder mappingBinder = new MappingBinder(cls, true);
    final InputStream inputStream = cls.locateResourceStream("xml/jakarta/simple/orm.xml");
    try {
        final Binding<JaxbEntityMappings> binding = mappingBinder.bind(new StreamSource(inputStream), new Origin(SourceType.RESOURCE, "xml/jakarta/simple/orm.xml"));
        assertThat(binding.getRoot().getEntity().stream().map(JaxbEntity::getClazz)).containsOnly("Lighter", "ApplicationServer");
        final JaxbPersistenceUnitMetadata puMetadata = binding.getRoot().getPersistenceUnitMetadata();
        final JaxbPersistenceUnitDefaults puDefaults = puMetadata.getPersistenceUnitDefaults();
        final Stream<String> listenerNames = puDefaults.getEntityListeners().getEntityListener().stream().map(JaxbEntityListener::getClazz);
        assertThat(listenerNames).containsOnly("org.hibernate.jpa.test.pack.defaultpar.IncrementListener");
    } finally {
        try {
            inputStream.close();
        } catch (IOException ignore) {
        }
    }
}
Also used : Origin(org.hibernate.boot.jaxb.Origin) InputStream(java.io.InputStream) JaxbEntityListener(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityListener) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) MappingBinder(org.hibernate.boot.jaxb.internal.MappingBinder) JaxbEntityMappings(org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings) JaxbPersistenceUnitDefaults(org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitDefaults) JaxbPersistenceUnitMetadata(org.hibernate.boot.jaxb.mapping.spi.JaxbPersistenceUnitMetadata) ClassLoaderService(org.hibernate.boot.registry.classloading.spi.ClassLoaderService) JaxbEntity(org.hibernate.boot.jaxb.mapping.spi.JaxbEntity) Test(org.junit.jupiter.api.Test)

Aggregations

JaxbEntityMappings (org.hibernate.boot.jaxb.mapping.spi.JaxbEntityMappings)8 Origin (org.hibernate.boot.jaxb.Origin)3 XMLContext (org.hibernate.cfg.annotations.reflection.internal.XMLContext)3 XMLMappingHelper (org.hibernate.orm.test.internal.util.xml.XMLMappingHelper)3 JaxbHbmHibernateMapping (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping)2 Test (org.junit.jupiter.api.Test)2 NamedNativeQuery (jakarta.persistence.NamedNativeQuery)1 NamedQuery (jakarta.persistence.NamedQuery)1 NamedStoredProcedureQuery (jakarta.persistence.NamedStoredProcedureQuery)1 SequenceGenerator (jakarta.persistence.SequenceGenerator)1 SqlResultSetMapping (jakarta.persistence.SqlResultSetMapping)1 TableGenerator (jakarta.persistence.TableGenerator)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 StreamSource (javax.xml.transform.stream.StreamSource)1 UnsupportedOrmXsdVersionException (org.hibernate.boot.UnsupportedOrmXsdVersionException)1 MappingBinder (org.hibernate.boot.jaxb.internal.MappingBinder)1