javax.persistence.PersistenceException: No Persistence provider for EntityManager named tomcat - java

I trying to use hibernate as JPA implementation in my web application. I have spent several hours dealing with this problem. I have following directory structure http://i62.tinypic.com/345bqlt.png.
Here is content of my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_1_0.xsd"
version="1.0">
<persistence-unit name="myName" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://xxxxxxxx"/>
<property name="hibernate.connection.username" value="xxxxxxx"/>
<property name="hibernate.connection.password" value="xxxxxxx"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
I am trying to access database from servlet
EntityManager entityManager = Persistence.createEntityManagerFactory("myName").createEntityManager();
CustomerProvider customerProvider = new CustomerProvider(entityManager);
Customer customer = customerProvider.get(9);
I am working in Intellij IDEA. I have previous simple Maven Hibernate project and it works, but in web app it doesn't seems to work.
I am getting such exception trying to access servlet.
javax.persistence.PersistenceException: No Persistence provider for EntityManager named myName
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
controller.TestServlet.doGet(TestServlet.java:24)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Please !!! Help to solve this problem. I have to work, but cannot continue due this problem. Maybe it is incorrect to access database through dao in servlet, please suggest right way than.
You are my hope only.

Related

javax.persistence.PersistenceException: No resource files named META-INF/services/javax.persistence.spi.PersistenceProvider were found

I know this question has been asked but none of the answers really solved my problem.
I need to add this persistence unit to the manager.
Thanks for answers in advance
I have this project structure
And this piece of code throws the exception in the title.
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("ApplicationPU");
EntityManager em = emf.createEntityManager();
My persistence persistence.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="ApplicationPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>entity.Kategorie</class>
<properties>
<property
name="javax.persistence.jdbc.url"
value="jdbc:postgresql://slon.felk.cvut.cz:5432/database"/>
<property
name="javax.persistence.jdbc.user"
value="user"/>
<property
name="javax.persistence.jdbc.driver"
value="org.postgresql.Driver"/>
<property
name="javax.persistence.jdbc.password"
value="password"/>
<property
name="javax.persistence.schema-generation.database.action"
value="create"/>
</properties>
</persistence-unit>
</persistence>
The whole exception looks like this:
javax.persistence.PersistenceException: No resource files named META-INF/services/javax.persistence.spi.PersistenceProvider were found. Please make sure that the persistence provider jar file is in your classpath.
at javax.persistence.Persistence.findAllProviders(Persistence.java:167)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:103)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at Main.main(Main.java:17)

Hibernate JPA - No Persistence provider for EntityManager error

I am trying to connect to sql server using JPA and hibernate with an Eclipse Maven project. I've tried moving the persistence.xml file to various locations but it doesn't seem to help.
My persistence.xml file is
<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="Trades" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver:/Trades\SQL00;databaseName=Trades" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="dev" />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="show_sql" value="true"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</properties>
</persistence-unit>
</persistence>
and my code is as follows and is located in src/main/resources/META-INF
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Trades");
System.out.println();
}
I have attached a screenshot which shows my eclipse set up
I solved this, turned out that Maven must have downloaded corrupt libraries - I was alerted this when I was unable to view the source for some classes in the Hibernates jars. I deleted the jars in my .m2 directory and when the jars were refreshed, it worked fine. A bit of tricky one.

How to implement JPA in Play Framework in a standard way

I have gone though https://www.playframework.com/documentation/2.5.x/JavaJPA
But it doesn't feel like a stardard way. I would like fill my jdbc connection in persistence.xml instead of application.conf
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="TestPersistence"
transaction-type="RESOURCE_LOCAL">
<class>com.example.pojo.Employee</class>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/jpadb" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password"
value="mukesh" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
instead of filling the jdbc connection in application.conf like
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/jpadb"
db.default.user=root
db.default.password="mukesh"
db.default.jndiName=DefaultDS
jpa.default=TestPersistence
Also if there is no other way to get jdbc connection in persistence.xml how can we define more than one jdbc connection in application.conf. like if i have a mysql for users and oracle db for posts.
NOTE: the source code posted are just dummy copied from public domain to show the briefly problem.
You don't have to use Play's DB module. You can have your own module defined, and load your database connection and run it in isolation.
I would check out https://github.com/playframework/play-isolated-slick as an example -- here the UserDAO is bound through to SlickDAO in the Play module:
https://github.com/playframework/play-isolated-slick/blob/master/modules/play/app/Module.scala#L19
and the database provider is provided:
https://github.com/playframework/play-isolated-slick/blob/master/modules/play/app/Module.scala#L27
but you don't have to use Database.forConfig("myapp.database") -- you can define any config you feel like.

javax.persistence.PersistenceException: No Persistence provider for EntityManager named

Hello I am new in Java Web and I have a problem that I get this communicate
javax.persistence.PersistenceException: No Persistence provider for EntityManager named NaszSerwisPU
Here is my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<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_1_0.xsd" version="1.0">
<persistence-unit name="NaszSerwisPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="passsword"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/naszserwis"/>
<property name="hibernate.max_fetch_depth" value="3"/>
</properties>
</persistence-unit>
</persistence>
I try to call:
public String logIn() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("NaszSerwisPU");
EntityManager mgr = entityManagerFactory.createEntityManager();
User us = new User();
us.setLogin("admin");
us.setPassword("admin");
mgr.persist(us);
return "/main.xhtml";
}
I don`t know why i get this communicate.
I use NetBeans
UPDATE:
I had removed hibernate.cfg.xml and changed
<provider>org.hibernate.ejb.HibernatePersistenceProvider></provider>
to:
<provider>org.hibernate.jpa.HibernatePersistenceProvider></provider>
but problem still exist, communicate of error is the same.
You do not need hibernate.cfg.xml if you use JPA and persistence.xml
I believe you should write
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
Make sure that persistence.xml file is in the right location.
See this Persistence.xml where to put in eclipse project

How to define an non-JTA datasource for JPA-Hibernate? org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource:

I am trying to change my web-app's JDBC code to JPA using Hibernate as provider. I am using Eclipse IDE. In that i have defined a MySQL data source. I added it in the persistence.xml.
But, I am getting the below error.
6640 [30289364#qtp-7494106-7] ERROR org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: tamSql
javax.naming.NameNotFoundException; remaining name 'tamSql'
My persistence.xml looks like,
<?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="ExpensePersistentUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>tamSql</non-jta-data-source>
<class>com.pricar.JPAInteg.Role</class>
<class>com.pricar.JPAInteg.User</class>
<class>com.pricar.JPAInteg.Userdetail</class>
<class>com.pricar.JPAInteg.Category</class>
<class>com.pricar.JPAInteg.Expens</class>
<class>com.pricar.JPAInteg.Leavetable</class>
<class>com.pricar.JPAInteg.Permissiontoken</class>
<class>com.pricar.JPAInteg.Roletokenassociation</class>
<class>com.pricar.JPAInteg.UserPK</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/officemgmt"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
Any Suggestions!!!
Thanks in Advance!
You don't need <non-jta-data-source> when your datasource is configured in <properties>. <non-jta-data-source> is needed when datasource is configured in application server configuration and obtained via JNDI.

Categories

Resources