我正在尝试在Wildfly上运行一个非常基本的JPA示例。 这是我项目的结构:
Person和Project是两个JPA实体。 这是我的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="testJpa">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>entities.Person</class>
<class>entities.Project</class>
<properties>
<property name="eclipselink.target-database" value="Derby"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/JPADB"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="pwd"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
从实体类中正确生成表,因此我知道persistence.xml是有效的。
我正在尝试在CreatePerson类中创建一个实体,如下所示:
public class CreatePerson {
public static void main( String[ ] args ) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("testJpa");
EntityManager entitymanager = emfactory.createEntityManager( );
entitymanager.getTransaction( ).begin( );
Person usr = new Person( );
usr.setName("Bob");
entitymanager.persist( usr );
entitymanager.getTransaction( ).commit( );
entitymanager.close( );
emfactory.close( );
}
}
我收到以下错误
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named testJpa
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at services.CreatePerson.main(CreatePerson.java:11)
答案 0 :(得分:0)
目前还不清楚你在做什么。 Wildfly是一个JavaEE应用程序服务器,但我看到JavaSE类 public static void main 。
你应该创建JavaEE项目(对于Eclipse,据我所知你需要下载Eclipse IDE for Java EE Developers)然后可能创建DAO EJB,在其中注入 EntityManager
@PersistenceContext(unitName = "puName")
private EntityManager em;
然后你的代码应该工作
Person usr = new Person("blabla");
em.persist(usr)
恕我直言,你应该寻找另一个教程。