I have my persistence.xml with the same name, using toplink, under META-INF directory. Then I have my code calling it with...
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;
}
}
I have got the following error message:
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)
Here are my persistence.xml - I think it should be right
<?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>
And there is a 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>
I very much appreciate your help - I'm doing in netbeans as a maven project
The argument for Persistence.createEntityManagerFactory
is the name of the persistence unit. According to your persistence.xml
your persistence unit is named cvut.fel.pjv_LibrarySystem_server_jar_1.0-SNAPSHOTPU . The following code should work:
emf = Persistence.createEntityManagerFactory("cvut.fel.pjv_LibrarySystem_server_jar_1.0-SNAPSHOTPU");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.