没有名为 EntityManager 的持久性提供程序

2022-08-31 07:11:26

我有我的同名使用在目录下。然后,我的代码调用它:persistence.xmlTopLinkMETA-INF

EntityManagerFactory emfdb = Persistence.createEntityManagerFactory("agisdb");

但是,我收到以下错误消息:

2009-07-21 09:22:41,018 [main] ERROR - No Persistence provider for EntityManager named agisdb
javax.persistence.PersistenceException: No Persistence provider for EntityManager named agisdb
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:89)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)

这是:persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="agisdb">
        <class>com.agis.livedb.domain.AddressEntity</class>
        <class>com.agis.livedb.domain.TrafficCameraEntity</class>
        <class>com.agis.livedb.domain.TrafficPhotoEntity</class>
        <class>com.agis.livedb.domain.TrafficReportEntity</class>
        <properties>
            <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/agisdb"/>
            <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="toplink.jdbc.user" value="root"/>
            <property name="toplink.jdbc.password" value="password"/>
        </properties>
    </persistence-unit>
</persistence>

它应该在类路径中。然而,我得到了上面的错误。


答案 1

将 放在应用程序的类路径中。
对于较新版本,应使用而不是已弃用的版本"hibernate-entitymanager.jar""hibernate-core.jar"hibernate-entitymanager

如果你正在运行一些IDE,比如EclipseProject Properties -> Java Build Path -> Libraries.

否则,请将其放入应用程序中。/lib


答案 2

在 之后,定义持久性提供程序名称:<persistence-unit name="agisdb">

<provider>org.hibernate.ejb.HibernatePersistence</provider>

推荐