Single XML file for all class-mapping

Hi Folks,

I am using Hibernate version 5.4.31 Final and in my hibernate.cfg.xml I have all the below annotated class models. Now I want to clean-up hibernate.cfg.xml and move all these mappings in myMappings.xml and put myMappings.xml somehow in hibernate.cfg.xml.

<mapping class="com.test.a.Model1" />
<mapping class="com.abc.test2.Model2" />
...................
........................
...................
<mapping class="com.as.er.Model3" />

Any help on how to achieve this?

What’s the problem? You can just move these definitions to a file and refer to that file by using <mapping resource="your-file.hbm.xml"/>

I have kept hibernate.cfg.xml and my ormMappings.hbm.xml file in the same location. But still I get the below error:

Caused by: org.hibernate.boot.InvalidMappingException: Could not parse mapping document: myApp-orm-mappings.hbm.xml (RESOURCE)
	at org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:46)
	at org.hibernate.boot.jaxb.internal.UrlXmlSource.doBind(UrlXmlSource.java:36)
	at org.hibernate.boot.spi.XmlMappingBinderAccess.bind(XmlMappingBinderAccess.java:59)
	at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:311)
	at org.hibernate.boot.cfgxml.spi.MappingReference.apply(MappingReference.java:70)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85)
	at com.app.orm.dao.HibernateUtil.<clinit>(HibernateUtil.java:47)
	... 36 more
Caused by: org.hibernate.boot.MappingException: An error occurred transforming orm.xml document from StAX to dom4j representation  : origin(myApp-orm-mappings.hbm.xml)
	at org.hibernate.boot.jaxb.internal.MappingBinder.toDom4jDocument(MappingBinder.java:111)
	at org.hibernate.boot.jaxb.internal.MappingBinder.doBind(MappingBinder.java:71)
	at org.hibernate.boot.jaxb.internal.AbstractBinder.doBind(AbstractBinder.java:103)
	at org.hibernate.boot.jaxb.internal.AbstractBinder.bind(AbstractBinder.java:58)
	at org.hibernate.boot.jaxb.internal.InputStreamXmlSource.doBind(InputStreamXmlSource.java:43)
	... 43 more
Caused by: com.ctc.wstx.exc.WstxParsingException: Illegal to have multiple roots (start tag in epilog?).
 at [row,col {unknown-source}]: [2,3]
	at com.ctc.wstx.sr.StreamScanner.constructWfcException(StreamScanner.java:606)
	at com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:479)
	at com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:464)
	at com.ctc.wstx.sr.BasicStreamReader.handleExtraRoot(BasicStreamReader.java:2140)
	at com.ctc.wstx.sr.BasicStreamReader.nextFromProlog(BasicStreamReader.java:2055)
	at com.ctc.wstx.sr.BasicStreamReader.closeContentTree(BasicStreamReader.java:2889)
	at com.ctc.wstx.sr.BasicStreamReader.nextFromTree(BasicStreamReader.java:2632)
	at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1065)
	at org.codehaus.stax2.ri.Stax2EventReaderImpl.peek(Stax2EventReaderImpl.java:367)
	at java.xml/javax.xml.stream.util.EventReaderDelegate.peek(EventReaderDelegate.java:98)
	at org.hibernate.boot.jaxb.internal.stax.BufferedXMLEventReader.peek(BufferedXMLEventReader.java:96)
	at java.xml/javax.xml.stream.util.EventReaderDelegate.peek(EventReaderDelegate.java:98)
	at org.hibernate.boot.jaxb.internal.stax.JpaOrmXmlEventReader.peek(JpaOrmXmlEventReader.java:65)
	at org.dom4j.io.STAXEventReader.readDocument(STAXEventReader.java:234)
	at org.hibernate.boot.jaxb.internal.MappingBinder.toDom4jDocument(MappingBinder.java:108)
	... 47 more

Do I need to wrap around <mapping class ="my classes" /> in some other tag?

Also, here is the HibernateUtil.java class

public class HibernateUtil {
	
	private HibernateUtil() {
		
	}
	
	 private static final SessionFactory sessionFactory;
	 static HibernateUtil hibernateUtil = null;
	 
	    // Hibernate 5:
	    static {
	        try {
	            // Create the ServiceRegistry from hibernate.cfg.xml
	            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()//
	                    .configure("hibernate.cfg.xml").build();
	 
	            // Create a metadata sources using the specified service registry.
	            Metadata metadata = new MetadataSources(serviceRegistry).getMetadataBuilder().build();
	 
	            sessionFactory = metadata.getSessionFactoryBuilder().build();
	        } catch (Throwable ex) {	         
	            System.err.println("Initial SessionFactory creation failed." + ex);
	            throw new ExceptionInInitializerError(ex);
	        }
	    }
	    
	    public synchronized static HibernateUtil getInstance(){
			if (hibernateUtil == null){
				hibernateUtil = new HibernateUtil();
			}
			return hibernateUtil;
		}
	 
	    public static SessionFactory getSessionFactory() {
	        return sessionFactory;
	    }
	 
	    public static void shutdown() {
	        // Close caches and connection pools
	        getSessionFactory().close();
	    }

}

As the exception tells you, your file can not be parsed.

Yes, but it says multiple root element found. I just have below entries in the file and no other tags


You didn’t share any content… but anyway, the file should look similar to the following:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="YourClass">
    <!-- Your definitions -->
    </class>

</hibernate-mapping>

In case you are using the annotation model, you should switch to the JPA orm.xml descriptors instead.