I am creating a server in Glassfish and I want to have a JPA persistence layer to a remote MySQL database.
When I attempt to use the persistence layer, I get this exception: javax.persistence.PersistenceException: No Persistence provider for EntityManager named em1
Based on other StackOverflow posts, it seems this is an indication the persistence.xml is invalid. My persistence.xml is shown. Can someone point me in the right direction to figuring out how to get this to work?
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name ="em1">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>data.entry.Movie</class>
<properties>
<property name="eclipselink.target-database"
value="MySQL4"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="mysql3.cs.myschool.edu/mydb"/>
<property name="javax.persistence.jdbc.user" value="mydb" />
<property name="javax.persistence.jdbc.password" value="mypass" />
</properties>
</persistence-unit>
</persistence>
Related
Getting the error "No Persistence provider for EntityManager named" while trying to run Java Maven project in IntelliJ, the project runs fine in Visual studio Code with no errors or warnings
i'm not fully sure what code be causing the issue
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named default
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at JNWR.ServerApplication.<clinit>(ServerApplication.java:18)
here is my persistence file
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2.xsd"
version="2.2">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<description>
Persistence unit for the JNWR Database
</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>JNWR.Entity.Customer</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.cj.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/jwrms_db" />
<property name="javax.persistence.jdbc.user"
value="defaultUser"/>
<property name="javax.persistence.jdbc.password"
value="defaultPassword%"/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL8Dialect" />
<property name="hibernate.show_sql"
value="true" />
</properties>
</persistence-unit>
</persistence>
Already tried:
Adding META-INF to classpath manually,
moved it into the root folder,
moved it into resources folder,
moved persistence.xml to root, resources and of course META-INF.
Imported the project into vscode (which worked but I want it to work in IntelliJ)
I'm trying to integrate Spring with JPA. I have defined one entity and added as mapping file under persistence.xml, but it throws the exception
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: MyPersistence] Unable to resolve named mapping-file [net.bb.spring.entities.DealDetailsEntity]
My persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/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">
<persistence-unit name="MyPersistence">
<description>AS400 Configuration for BB</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<mapping-file>net.bb.spring.entities.DealDetailsEntity</mapping-file>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.ibm.as400.access.AS400JDBCDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:as400://172.21.100.101/BB" />
<property name="javax.persistence.jdbc.user" value="ROOT" />
<property name="javax.persistence.jdbc.password" value="ROOT" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
From oracle docs about persistence.xml file:
<mapping-file> element: specifies one or more object-relational
mapping XML files (orm.xml files).
What your <mapping-file> is pointing at is an entity which is a java class. This should not pass to <mapping-file> but to a <class> tag instead.
e.g.
<class>net.bb.spring.entities.DealDetailsEntity</class>
I am trying to understand all the properties for schemas generation of JPA 2.1 using EclipseLink implementation and a Mysql database, but I had several bugs easy to explain but difficult to debug
Here is my persistence.xml document
<?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="fairHandlerPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>nrz.fairHandler.jpa.Unit</class>
<class>nrz.fairHandler.jpa.Weight</class>
<class>nrz.fairHandler.jpa.Pruduct</class>
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/fairhandlerdb?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.schema-generation.create-database-schemas" value="true"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
Everything work fine in execution if the schema 'fairhandlerdb' is already created, whether the 'fairhandlerdb' tables exists or not, however, if the schema don't exist it will return an exception at runtime:
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'fairhandlerdb'
which for me is a contradiction since this property job
<property name="javax.persistence.schema-generation.create-database-schemas" value="true"/>
is to create schema if this one does not exist.
After that, I tried to resolve the problem in another way by executing a creating schemas script, by adding these properties:
<property name="javax.persistence.schema-generation.script.create-source" value="script-then-metadata"/>
<property name="javax.persistence.schema-generation.script.drop-source" value="metadata"/>
<property name="javax.persistence.schema-generation.script.create-script-source" value="META-INF/createSchema.sql"/>
where createSchema.sql contain only one query:
CREATE SCHEMA `fairhandlerdb` ;
But it does not work neither when the database schema does not already exist at execution time.
My question is, why the two methods used above, didn't work for generating the database schema?
Thank you for your help
I am trying to learn JPA with Hibernate implementation. There are lot of blog of net about this but still i am struggling to implement it. I have written one Dao class which creates the EntityManagerFactory. But while looking this PERSISTENCE_UNIT it says
javax.persistence.PersistenceException: No Persistence provider for EntityManager named test
private final String PERSISTENCE_UNIT = "test";
private EntityManager entityManager;
public GenericDao() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory(PERSISTENCE_UNIT);
entityManager = factory.createEntityManager();
}
I also created one persistence.xml file which is in META-INF folder of web application. It looks like this.
<?xml version="1.0" encoding="UTF-8" ?>
<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="test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgres://localhost:1532/test" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
For deployment Tomcat8 is being used. Please let me know why application not able to lookup PERSISTENCE_UNIT.
As per comment - the problem is your persistence.xml location. It needs to be exactly where your application expects it to be:
WEB-INF/classes/META-INF/persistence.xml
Now, why did you encounter this problem is another matter, connected with how you create your .war file. Solution that should work however, is trusting Maven to take care of that. If you set packaging to war, and put your META-INF folder into resources (src/main/resources/META-INF/persistence.xml) it should take care of things.
I tried to get Spring-MVC, Hibernate, JTA with a Postgres Server to work.
I got most of the stuff working (Read from Db through EntityManager without JTA), but I can't get the transactions to work with JTA.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/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">
<persistence-unit name="defaultPersistenceUnit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>???</jta-data-source>
<class>net.test.test.database.UsersEntity</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/mydb" />
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="validpw"/>
</properties>
</persistence-unit>
If I provide my datasource from my servlet I get an Exception like
DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'dataSource'
I also found resources saying the datasource should be defined in the application-server (but I am not sure about this one). If it is important I use a Tomcat-server.