I am using JPA and I tried to put my configuration data in the persistence.xml file. But when I run the app, the error is as follows:
[hibernatetool] org.hibernate.MappingException: invalid configu开发者_运维问答ration
[hibernatetool] org.xml.sax.SAXParseException: Document is invalid: no grammar found.
And I googled it. It seems to say I have missed the DOCTYPE part. Like:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
So I tried to add the missing DOCTYPE part. But new question comes. WHAT should I add?
<?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: I have downdloaded several jpa sample projects and checked the associated persistence.xml files but found that they are all absent of the DOCTYPE part.
Is the DOCTYPE part necessary, I have to ask, because so many code samples are absent from it.
The solution for my problem seems to be: what should I put for the DOCTYPE part.
You miss the properties-element, which is child of persistence-unit and parent for property-elements.
<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>
精彩评论