0

我正在使用 JPA,并尝试将我的配置数据放在persistence.xml文件中。但是当我运行该应用程序时,错误如下:

[hibernatetool] org.hibernate.MappingException: invalid configuration
[hibernatetool] org.xml.sax.SAXParseException: Document is invalid: no grammar found.

我用谷歌搜索了它。似乎说我错过了DOCTYPE部分。喜欢:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

所以我尝试添加缺少的 DOCTYPE 部分。但是新的问题来了。我应该添加什么?

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">
    <persistence-unit name="helloworld">
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/db" />
        <property name="hibernate.connection.username" value="*****" />
        <property name="hibernate.connection.password" value="*****" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />

    </persistence-unit>
</persistence>

PS: 我已经下载了几个jpa示例项目并检查了相关的persistence.xml文件,但发现它们都没有DOCTYPE部分。

我不得不问 DOCTYPE 部分是否必要,因为它缺少很多代码示例。

我的问题的解决方案似乎是: 我应该为 DOCTYPE 部分添加什么。

4

1 回答 1

0

您错过了属性元素,它是持久性单元的子元素和属性元素的父元素。

<persistence-unit name="helloworld">
    <properties>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/db" />
          ...
    </properties>
</persistence-unit>
于 2011-09-30T12:07:37.557 回答