gpt4 book ai didi

java - intellij自动项目中hibernate xml解析嵌套异常

转载 作者:行者123 更新时间:2023-12-01 15:02:25 25 4
gpt4 key购买 nike

我在解析 hibernate xml 时遇到问题,这是由 intellij 使用此文件生成的自动项目,但对于我已添加到 UserEntity.hbm.xml 的文档类型

我来回更改了 xsd 到 dtd 但仍然出现异常我的主要

public class Main {
private static final SessionFactory ourSessionFactory;
private static ServiceRegistry serviceRegistry;

static {
try {
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
ourSessionFactory = configuration.buildSessionFactory(serviceRegistry);

} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}

public static Session getSession() throws HibernateException {
return ourSessionFactory.openSession();
}

public static void main(final String[] args) throws Exception {
final Session session = getSession();
try {
System.out.println("querying all the managed entities...");
final Map metadataMap = session.getSessionFactory().getAllClassMetadata();
for (Object key : metadataMap.keySet()) {
final ClassMetadata classMetadata = (ClassMetadata) metadataMap.get(key);
final String entityName = classMetadata.getEntityName();
final Query query = session.createQuery("from " + entityName);
System.out.println("executing: " + query.getQueryString());
for (Object o : query.list()) {
System.out.println(" " + o);
}
}
} finally {
session.close();
}
}

}

我的 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="dao.UserEntity"/>
<mapping resource="dao/UserEntity.hbm.xml"/>
<!--<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>-->
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>

我的 UserEntity.hbm.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping XSD//EN"
"http://www.hibernate.org/xsd/hibernate-mapping">
<hibernate-mapping xmlns="http://www.hibernate.org/xsd/hibernate-mapping"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping http://www.hibernate.org/xsd/hibernate-mapping/hibernate-mapping-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<class name="dao.UserEntity" table="user" schema="" catalog="test">
<id name="id">
<column name="id" sql-type="int" length="10" not-null="true"/>
</id>
<property name="path">
<column name="path" sql-type="varchar" length="25"/>
</property>
</class>

我的堆栈跟踪

Nov 19, 2012 7:39:00 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Nov 19, 2012 7:39:00 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.1}
Nov 19, 2012 7:39:00 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 19, 2012 7:39:00 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 19, 2012 7:39:00 AM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Nov 19, 2012 7:39:00 AM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Nov 19, 2012 7:39:00 AM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: dao/UserEntity.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
at Main.<clinit>(Main.java:32)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Caused by: org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:106)
at org.hibernate.cfg.Configuration.add(Configuration.java:477)
at org.hibernate.cfg.Configuration.add(Configuration.java:473)
at org.hibernate.cfg.Configuration.add(Configuration.java:646)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:729)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2105)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2077)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2057)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2010)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1925)
at Main.<clinit>(Main.java:27)
... 3 more
Caused by: org.dom4j.DocumentException: http://www.hibernate.org/xsd/hibernate-mapping Nested exception: http://www.hibernate.org/xsd/hibernate-mapping
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:76)
... 13 more

最佳答案

包裹你的class映射于<hibernate-mapping>标签如下:

<hibernate-mapping>
<class name="dao.UserEntity" table="user" schema="" catalog="test">
<id name="id">
<column name="id" sql-type="int" length="10" not-null="true"/>
</id>
<property name="path">
<column name="path" sql-type="varchar" length="25"/>
</property>
</class>
</hibernate-mapping>

另外,我认为,id属性应该有 generator类映射也是如此。

关于java - intellij自动项目中hibernate xml解析嵌套异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13448339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com