【问题标题】:JPA: No Persistence provider for Entity Manager: using Intellij community edition and PostgresqlJPA:实体管理器没有持久性提供程序:使用 Intellij 社区版和 Postgresql
【发布时间】:2015-02-20 17:13:27
【问题描述】:

我正在使用 Intellij 社区版学习 JPA。我读过它可以与这个 Intellij 版本一起使用。

我已经使用 pgAdmin 在 localhost 上创建了我的数据库。我已将外部 jars 添加到我的项目中(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)

【问题讨论】:

  • 你能发布你的文件夹结构吗?

标签: java postgresql jpa intellij-idea


【解决方案1】:

您是否检查了工件布局,persistence.xml 应该位于 /WEB-INF/classes/META-INF/persistence.xml 下。据我所知,Intellij 社区不会自动检测配置文件。

【讨论】:

    【解决方案2】:

    你需要定义JPA的提供者,因为JPA只是一个规范。最常见的是 Hibernate 和 EclipseLink。

    这是标签:

    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    

    用你想要或需要的任何东西替换提供者。

    【讨论】:

    • 这不是错误:没有名为 testname 的 EntityManager 的持久性提供程序
    猜你喜欢
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    相关资源
    最近更新 更多