Search in sources :

Example 1 with JaxbHbmHibernateMapping

use of org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping in project hibernate-orm by hibernate.

the class AdditionalJaxbMappingProducerImpl method produceAdditionalMappings.

@Override
public Collection<MappingDocument> produceAdditionalMappings(final MetadataImplementor metadata, IndexView jandexIndex, final MappingBinder mappingBinder, final MetadataBuildingContext buildingContext) {
    final ServiceRegistry serviceRegistry = metadata.getMetadataBuildingOptions().getServiceRegistry();
    final EnversService enversService = serviceRegistry.getService(EnversService.class);
    if (!enversService.isEnabled()) {
        // short-circuit if envers integration has been disabled.
        return Collections.emptyList();
    }
    final ArrayList<MappingDocument> additionalMappingDocuments = new ArrayList<>();
    // atm we do not have distinct origin info for envers
    final Origin origin = new Origin(SourceType.OTHER, "envers");
    //		final DOMWriter writer = new DOMWriter();
    final MappingCollector mappingCollector = new MappingCollector() {

        @Override
        public void addDocument(Document document) throws DocumentException {
            dump(document);
            // while the commented-out code here is more efficient (well, understanding that
            // this whole process is un-efficient)  it leads to un-decipherable messages when
            // we get mapping mapping errors from envers output.
            //				final DOMSource domSource = new DOMSource( writer.write( document ) );
            //				domSource.setSystemId( "envers" );
            //				final Binding jaxbBinding = mappingBinder.bind( domSource, origin );
            // this form at least allows us to get better error messages
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                final Writer w = new BufferedWriter(new OutputStreamWriter(baos, "UTF-8"));
                final XMLWriter xw = new XMLWriter(w, new OutputFormat(" ", true));
                xw.write(document);
                w.flush();
            } catch (IOException e) {
                throw new HibernateException("Unable to bind Envers-generated XML", e);
            }
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            BufferedInputStream bis = new BufferedInputStream(bais);
            final Binding jaxbBinding = mappingBinder.bind(bis, origin);
            final JaxbHbmHibernateMapping jaxbRoot = (JaxbHbmHibernateMapping) jaxbBinding.getRoot();
            additionalMappingDocuments.add(new MappingDocument(jaxbRoot, origin, buildingContext));
        }
    };
    enversService.initialize(metadata, mappingCollector);
    return additionalMappingDocuments;
}
Also used : Origin(org.hibernate.boot.jaxb.Origin) Binding(org.hibernate.boot.jaxb.spi.Binding) HibernateException(org.hibernate.HibernateException) MappingDocument(org.hibernate.boot.model.source.internal.hbm.MappingDocument) ArrayList(java.util.ArrayList) OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Document(org.dom4j.Document) MappingDocument(org.hibernate.boot.model.source.internal.hbm.MappingDocument) XMLWriter(org.dom4j.io.XMLWriter) BufferedWriter(java.io.BufferedWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) OutputStreamWriter(java.io.OutputStreamWriter) ServiceRegistry(org.hibernate.service.ServiceRegistry) MappingCollector(org.hibernate.envers.configuration.internal.MappingCollector) XMLWriter(org.dom4j.io.XMLWriter) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) JaxbHbmHibernateMapping(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping)

Example 2 with JaxbHbmHibernateMapping

use of org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping in project hibernate-orm by hibernate.

the class EntityHierarchyBuilder method indexMappingDocument.

/**
	 * Called for each mapping document.
	 *
	 * @param mappingDocument The {@code hbm.xml} document to index
	 */
public void indexMappingDocument(MappingDocument mappingDocument) {
    log.tracef("Indexing mapping document [%s] for purpose of building entity hierarchy ordering", mappingDocument.getOrigin());
    final JaxbHbmHibernateMapping mappingBinding = mappingDocument.getDocumentRoot();
    // iterate all root class definitions at the hibernate-mapping level
    for (JaxbHbmRootEntityType jaxbRootEntity : mappingBinding.getClazz()) {
        // we can immediately handle <class/> elements in terms of creating the hierarchy entry
        final RootEntitySourceImpl rootEntitySource = new RootEntitySourceImpl(mappingDocument, jaxbRootEntity);
        entitySourceByNameMap.put(rootEntitySource.getEntityNamingSource().getEntityName(), rootEntitySource);
        final EntityHierarchySourceImpl hierarchy = new EntityHierarchySourceImpl(rootEntitySource);
        entityHierarchyList.add(hierarchy);
        linkAnyWaiting(mappingDocument, rootEntitySource);
        // process any of its nested sub-entity definitions
        processRootEntitySubEntityElements(mappingDocument, jaxbRootEntity, rootEntitySource);
    }
    // iterate all discriminator-based subclass definitions at the hibernate-mapping level
    for (JaxbHbmDiscriminatorSubclassEntityType discriminatorSubclassEntityBinding : mappingBinding.getSubclass()) {
        processTopLevelSubClassBinding(mappingDocument, discriminatorSubclassEntityBinding);
    }
    // iterate all joined-subclass definitions at the hibernate-mapping level
    for (JaxbHbmJoinedSubclassEntityType joinedSubclassEntityBinding : mappingBinding.getJoinedSubclass()) {
        processTopLevelSubClassBinding(mappingDocument, joinedSubclassEntityBinding);
    }
    // iterate all union-subclass definitions at the hibernate-mapping level
    for (JaxbHbmUnionSubclassEntityType unionSubclassEntityBinding : mappingBinding.getUnionSubclass()) {
        processTopLevelSubClassBinding(mappingDocument, unionSubclassEntityBinding);
    }
}
Also used : JaxbHbmUnionSubclassEntityType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmUnionSubclassEntityType) JaxbHbmDiscriminatorSubclassEntityType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmDiscriminatorSubclassEntityType) JaxbHbmRootEntityType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmRootEntityType) JaxbHbmJoinedSubclassEntityType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmJoinedSubclassEntityType) JaxbHbmHibernateMapping(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping)

Example 3 with JaxbHbmHibernateMapping

use of org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping 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)) {
        log.debugf("Performing JAXB binding of hbm.xml document : %s", origin.toString());
        XMLEventReader hbmReader = new HbmEventReader(staxEventReader, xmlEventFactory);
        JaxbHbmHibernateMapping hbmBindings = jaxb(hbmReader, LocalSchema.HBM.getSchema(), hbmJaxbContext(), origin);
        return new Binding<JaxbHbmHibernateMapping>(hbmBindings, origin);
    } else {
        try {
            final XMLEventReader reader = new JpaOrmXmlEventReader(staxEventReader, xmlEventFactory);
            return new Binding<Document>(toDom4jDocument(reader, origin), origin);
        } catch (JpaOrmXmlEventReader.BadVersionException e) {
            throw new UnsupportedOrmXsdVersionException(e.getRequestedVersion(), origin);
        }
    }
}
Also used : Binding(org.hibernate.boot.jaxb.spi.Binding) 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 4 with JaxbHbmHibernateMapping

use of org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping in project hibernate-orm by hibernate.

the class EntityModeConverterTest method generateXml.

private JaxbHbmHibernateMapping generateXml(boolean includeEntityMode) throws Exception {
    JaxbHbmHibernateMapping hm = new JaxbHbmHibernateMapping();
    JaxbHbmRootEntityType clazz = new JaxbHbmRootEntityType();
    JaxbHbmTuplizerType tuplizer = new JaxbHbmTuplizerType();
    tuplizer.setClazz(DynamicMapEntityTuplizer.class.getCanonicalName());
    if (includeEntityMode) {
        tuplizer.setEntityMode(EntityMode.MAP);
    }
    clazz.getTuplizer().add(tuplizer);
    JaxbHbmSimpleIdType id = new JaxbHbmSimpleIdType();
    clazz.setId(id);
    hm.getClazz().add(clazz);
    return hm;
}
Also used : JaxbHbmSimpleIdType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSimpleIdType) JaxbHbmTuplizerType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmTuplizerType) JaxbHbmRootEntityType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmRootEntityType) JaxbHbmHibernateMapping(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping) DynamicMapEntityTuplizer(org.hibernate.tuple.entity.DynamicMapEntityTuplizer)

Example 5 with JaxbHbmHibernateMapping

use of org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping in project hibernate-orm by hibernate.

the class GenerationTimingConverterTest method testMashallAttributeWithNullGenerationTiming.

@Test
public void testMashallAttributeWithNullGenerationTiming() throws Exception {
    JaxbHbmHibernateMapping hm = new JaxbHbmHibernateMapping();
    JaxbHbmRootEntityType clazz = new JaxbHbmRootEntityType();
    JaxbHbmSimpleIdType id = new JaxbHbmSimpleIdType();
    JaxbHbmBasicAttributeType att = new JaxbHbmBasicAttributeType();
    att.setName("attributeName");
    clazz.getAttributes().add(att);
    clazz.setId(id);
    hm.getClazz().add(clazz);
    XmlBindingChecker.checkValidGeneration(hm);
}
Also used : JaxbHbmSimpleIdType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSimpleIdType) JaxbHbmRootEntityType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmRootEntityType) JaxbHbmBasicAttributeType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmBasicAttributeType) JaxbHbmHibernateMapping(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping) Test(org.junit.Test)

Aggregations

JaxbHbmHibernateMapping (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmHibernateMapping)5 JaxbHbmRootEntityType (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmRootEntityType)3 JaxbHbmSimpleIdType (org.hibernate.boot.jaxb.hbm.spi.JaxbHbmSimpleIdType)2 Binding (org.hibernate.boot.jaxb.spi.Binding)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedWriter (java.io.BufferedWriter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 Document (org.dom4j.Document)1 OutputFormat (org.dom4j.io.OutputFormat)1 XMLWriter (org.dom4j.io.XMLWriter)1 HibernateException (org.hibernate.HibernateException)1 UnsupportedOrmXsdVersionException (org.hibernate.boot.UnsupportedOrmXsdVersionException)1 Origin (org.hibernate.boot.jaxb.Origin)1