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

标签 java hibernate intellij-idea

我在解析 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/

相关文章:

java - 运行来自 IntelliJ IDEA 的 @Category 注解的方法

java - Hibernate @OneToOne 和 ConstraintViolationException 关系的一侧

java - 延迟初始化异常 : load lazy attribute from a session different from the original one in Hibernate

intellij-idea - 当 Intellij IDEA 未安装在 C 上时,sonarlint 不起作用 : drive in windows

java - JaCoCo 与 IntelliJ

java - Play Framework : How to share models between different projects?

java - 如何为 Hadoop 打包 Java 程序

java - 您将如何以编程方式从存储在字符串中的日期创建模式?

java - 填充给定形状的选项数

java - 如何在 Mockito 中模拟 CompletableFuture 的完成