2016-04-26 9 views
0

Ich verwende dynamische Modelle für die Entity-Erstellung und ich muss Caching darüber bereitstellen. Aber ich erhalte die Ausnahme "XML kann nicht gelesen werden"Dynamisches Modell mit 2nd Level Cache in Hibernate

Schritt 1. Erstellen einer dynamischen Modelleinheit. Läuft gut. Ich kann Update mit dieser Entität speichern.

Die Entität wird zur Laufzeit erstellt.

Schritt 2. Geben Sie das Update in cfg xml an. Läuft gut, es erstellt auch die Tabelle, wenn es nicht existiert.

  1. Jetzt muss ich 2nd Level Cache auf dieser Entität bereitstellen, aber zuerst ich keine Dokumentation dafür sofort finden. Die zweite Angabe in der dynamischen Modelleinheit gibt die Ausnahme wie folgt. (Das Erstellen von Entitätsklassen anstelle von dynamischen Modellen läuft problemlos mit dieser Zeile ohne Ausnahmen, bereits getestet).

Ausnahme-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) 

Ich brauche Hilfe dieses bei der Lösung. Wenn ein zusätzliches Code-Snippet erforderlich ist, dann teilen Sie uns dies bitte mit mit.

Antwort

0

Dies ist ein Beispiel Mapping-Datei kompatibel mit beiden Hibernate 3. Ich hoffe, dies wird Ihnen helfen.

<?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> 

Bei Hibernate 5 ändern Sie einfach den Speicherort der Zuordnungsdatei. http://hibernate.org/dtd/hibernate-mapping-3.0.dtd

Für Second-Level-Cache wie ehcache, können Sie diese in hibernate.cfg.xml Datei verwenden

<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> 

Weitere Details finden: http://www.tutorialspoint.com/hibernate/hibernate_caching.htm

Verwandte Themen