我正在学习在日食中使用JPA 这是我的简单实体:
package it.test.simplejpatest;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="HELLO")
public class HelloEntity implements Serializable{
private static final long serialVersionUID = 5385853336067023611L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="HELLO_ID", nullable=false)
private int helloId;
@Column(name="MESSAGE")
private String message;
public int getHelloId() {
return helloId;
}
public void setHelloId(int helloId) {
this.helloId = helloId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
这是我的主要课程:
package it.test.simplejpatest;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class HelloWorld {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("simpleJPA");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
HelloEntity hello = new HelloEntity();
hello.setMessage("Gonzalo Higuain");
em.persist(hello);
tx.commit();
em.close();
emf.close();
}
}
最后,这是我的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="simpleJPA" transaction-type="RESOURCE_LOCAL">
<class>it.test.simplejpatest.HelloEntity</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/simplejpa"/>
<property name="javax.persistence.jdbc.user" value="simplejpauser"/>
<property name="javax.persistence.jdbc.password" value="simplejpauser"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
当我运行它时,我有这个例外:
Exception in thread "main" javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML (line-1 : column -1): cvc-elt.1: impossibile trovare la dichiarazione dell'elemento "persistence".
at org.hibernate.ejb.packaging.PersistenceXmlLoader.loadURL(PersistenceXmlLoader.java:147)
at org.hibernate.ejb.packaging.PersistenceXmlLoader.deploy(PersistenceXmlLoader.java:171)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:326)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:58)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at it.test.simplejpatest.HelloWorld.main(HelloWorld.java:11)
我不明白这个异常,因为我正确地将持久性单元名称放在main中。谁能帮我?感谢