我正在使用动态模型来创建实体,我需要提供 缓存它。但我得到例外“无法阅读XML”
步骤1.创建动态模型实体。运行很好。我可以保存更新 使用这个实体。
<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>
实体是在运行时创建的。
步骤2.在cfg中指定更新 XML。运行正常,如果它不存在,它也会创建表。
异常堆栈跟踪:
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)
我需要帮助来解决这个问题。如果需要任何额外的代码片段 让我知道。
答案 0 :(得分:0)
这是一个与hibernate 3兼容的示例映射文件。我希望这对你有帮助。
if (self.chatSections.count > 0) {
QMChatSection *currentSection = self.chatSections[indexPath.section];
if (currentSection.messages.count > 0){
return currentSection.messages[indexPath.item];
}else {
return nil;
}
} else {
return nil;
}
对于Hibernate 5,只需更改映射文件位置即可。 http://hibernate.org/dtd/hibernate-mapping-3.0.dtd
对于像ehcache这样的二级缓存,你可以在hibernate.cfg.xml文件中使用它
<?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>
有关详细信息,请参阅:http://www.tutorialspoint.com/hibernate/hibernate_caching.htm