我正在使用Intellij社区版学习JPA。我已经读过可以在Intellij版本中使用它。
我使用pgAdmin在localhost上创建了数据库。我已经将外部jar添加到我的项目中(JDBC Postresql驱动程序+ javax.presistance)。我使用persistence.xml文件(手动)META-INF文件夹:
<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"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="testname" transaction-type="RESOURCE_LOCAL">
<class>presistencetest.Car</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://host:localhost:5432/databasename"/>
<property name="javax.persistence.jdbc.user" value="postgresql"/>
<property name="javax.persistence.jdbc.password" value="password"/>
</properties>
</persistence-unit>
当我尝试启动简单测试(部分代码)时:
public static void main(String[] args) {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("testname");
EntityManager em = emf.createEntityManager();
我收到错误消息:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named testname
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:84)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at presistencetest.Car.main(Car.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
答案 0 :(得分:0)
您已经检查了工件布局,persistence.xml应该位于/WEB-INF/classes/META-INF/persistence.xml下。据我所知,Intellij Community不会自动检测配置文件。
答案 1 :(得分:-1)
您需要定义JPA的提供者,因为JPA只是一个规范。最常见的是Hibernate和EclipseLink。
这是标签:
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
将提供者替换为您想要或需要的任何内容。