我在META-INF目录下使用toplink创建了具有相同名称的persistence.xml。然后我用我的代码调用它......
public class DBConnect {
@PersistenceUnit
EntityManagerFactory emf;
public DBConnect()
{
emf = null;
}
public void startConnection()
{
emf = Persistence.createEntityManagerFactory
(
"cvut.fel.pjv.persistence.xml"
);
}
public void closeConnection()
{
emf.close();
}
public EntityManagerFactory getEMF()
{
return emf;
}
}
我收到以下错误消息:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named cvut.fel.pjv.persistence.xml
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at utils.DBConnect.startConnection(DBConnect.java:30)
at utils.Server.<init>(Server.java:32)
at utils.Server.main(Server.java:58)
这是我的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="cvut.fel.pjv_LibrarySystem_server_jar_1.0-SNAPSHOTPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>model.Vydani</class>
<class>model.Nosic</class>
<class>model.Vypujcka</class>
<class>model.Adresa</class>
<class>model.Casopis</class>
<class>model.Kategorie</class>
<class>model.Kniha</class>
<class>model.Nakladatelstvi</class>
<class>model.Zamestnanec</class>
<class>model.Ctenar</class>
<class>model.Vytisk</class>
<class>model.Autor</class>
<class>model.SpisovatelePocetDel</class>
<class>model.UzivateleAdresy</class>
<class>model.Osoba</class>
<class>model.DigitalniNosic</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://slon.felk.cvut.cz:5432/db18_koresmi1"/>
<property name="javax.persistence.jdbc.user" value="db18_koresmi1"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.password" value="Hk2JsA"/>
</properties>
</persistence-unit>
</persistence>
还有一个pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cvut.fel.pjv</groupId>
<artifactId>LibrarySystem_server</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
我非常感谢你的帮助 - 我在netbeans中做了一个maven项目
答案 0 :(得分:0)
Persistence.createEntityManagerFactory
的参数是持久性单元的名称。根据您的persistence.xml
,您的持久性单元名为 cvut.fel.pjv_LibrarySystem_server_jar_1.0-SNAPSHOTPU 。以下代码应该有效:
emf = Persistence.createEntityManagerFactory("cvut.fel.pjv_LibrarySystem_server_jar_1.0-SNAPSHOTPU");