我正在尝试使用JPA和EclipseLink来持久保存元素。所以我创建了一个类来坚持
@Entity
public class Couple implements Serializable{
private static final long serialVersionUID = 1L;
@Column(name = "OBJECTID")
private String objectID;
@Column(name = "EPCNUMBER")
private String epcNumber;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
等等。 我创建了一个“使用”类:
public class Main {
private static final String PERSISTENCE_UNIT_NAME = "MyPU";
private static EntityManagerFactory factory;
public static void main(String[] args) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
EntityManager em = factory.createEntityManager();
TypedQuery<Couple> q = em.createQuery("SELECT c FROM Couple c", Couple.class);
List<Couple> couples = (List<Couple>) q.getResultList();
然后,我有以下persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="MyPU">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>localJTA</jta-data-source>
<class>fr.mypackage.com.Couple</class>
<properties>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
但是,即使我更改属性,我也会遇到同样的错误:
线程“main”中的异常javax.persistence.PersistenceException:没有名为MyPU的EntityManager的持久性提供程序 (当调用factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);)。
我是否对Ling xml和persistence unit做错了什么?我已将weel添加到classpath中:
javax.persistence.jar
javax.ejb.jar
eclipselink.jar
javax.persistence_1.0.0.jar
javax.persistence_2.0.4.v201112161009.jar
derby.jar
你可以帮帮我吗?谢谢!
答案 0 :(得分:0)
在我的应用程序中,我遇到了这个问题,直到编译我的Netbeans项目。然后它奏效了。