0

I am using dynamic models for entity creation and i need to provide caching over it. But i am getting exception "Unable to read XML"

Step 1. Creating Dynamic model entity. Runs fine. I can save update using this entity.

<hibernate-mapping>

     <class entity-name="Customer">

         <id name="id"
             type="long"
             column="ID">
             <generator class="identity"/>
         </id>

         <property name="name"
             column="NAME"
             type="string"/>

         <property name="address"
             column="ADDRESS"
             type="string"/>

     </class>
</hibernate-mapping>

The entity is created at run time.

Step 2. Specify update in cfg xml. Runs fine, it creates the table as well if it does not exists.

  1. Now i need to provide 2nd level cache on this entity but first i do not find any documentation for it straightaway. Second specifying in dynamic model entity gives exception as below. (Creating entity classes instead of dynamic models runs fine with this line without exceptions, already tested).

Exception stack trace:

org.hibernate.InvalidMappingException: Unable to read XML
     at 
org.hibernate.internal.util.xml.MappingReader.legacyReadMappingDocument(MappingReader.java:375)
     at 
org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:304)
     at org.hibernate.cfg.Configuration.add(Configuration.java:516)
     at org.hibernate.cfg.Configuration.add(Configuration.java:512)
     at org.hibernate.cfg.Configuration.add(Configuration.java:686)
     at org.hibernate.cfg.Configuration.addResource(Configuration.java:769)
     at 
org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255)
     at 
org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227)
     at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207)
     at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160)
     at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
     at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:19)
     at util.HibernateUtil.getSessionFactory(HibernateUtil.java:37)
     at main.DynamicMain.main(DynamicMain.java:21)
Caused by: org.xml.sax.SAXParseException; lineNumber: 44; columnNumber: 
13; The content of element type "class" must match 
"(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,fetch-profile*,resultset*,(query|sql-query)*)".
     at 
org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown 
Source)

I need help in resolving this. If any extra code snippet required then let me know.

1 Answers1

0

This is an example mapping file compatible with both hibernate 3 .I hope this will help you.

<?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="com.ngp.dto.entityDTO"  table="ENTITY">
        <cache usage="read-only" />
        <id name="entityId" column="ID" length="30"><generator class="assigned"/></id>
        <property name="creationDate" column="CREATION_DATE" type="java.util.Date" update="false" not-null="true"/>
        <property name="name" column="NAME"  length="50" update="false" not-null="true"/>
        <property name="email" column="EMAIL"  length="50" update="false" not-null="true"/>
        <property name="organisation" column="ORGANISATION"  length="500" />
    </class>
</hibernate-mapping>

For Hibernate 5 , just change the mapping file location. http://hibernate.org/dtd/hibernate-mapping-3.0.dtd

For second level cache like ehcache , you may use this in hibernate.cfg.xml file

<property name="cache.use_query_cache">true</property> <property name="cache.use_second_level_cache">true</property> 
            <property name="cache.use_structured_entries">true</property> <property name="cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> 
            <property name="net.sf.ehcache.configurationResourceName">ehcache.xml</property>

For more details refer: http://www.tutorialspoint.com/hibernate/hibernate_caching.htm

Chetan chadha
  • 558
  • 1
  • 4
  • 19