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
Related
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.
I am getting the below error , when I test my application on oracle cloud. I am using Eclipse link but the below error is indicating openjpa.
<openjpa-1.1.1-SNAPSHOT-r422266:1445923 fatal user error> org.apache.openjpa.persistence.ArgumentException: A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property.
at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:72)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:776)
at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDBDictionaryInstance(JDBCConfigurationImpl.java:553)
at org.apache.openjpa.jdbc.meta.MappingRepository.endConfiguration(MappingRepository.java:1250)
at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:505)
at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:430)
at org.apache.openjpa.lib.conf.PluginValue.instantiate(PluginValue.java:103)
at org.apache.openjpa.conf.MetaDataRepositoryValue.instantiate(MetaDataRepositoryValue.java:68)
at org.apache.openjpa.lib.conf.ObjectValue.instantiate(ObjectValue.java:83)
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.newMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:843)
at org.apache.openjpa.conf.OpenJPAConfigurationImpl.getMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:834)
at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:638)
at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:183)
at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:142)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:199)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:152)
at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:56)
this is my persistence.xml
<?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="viamd" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>weblogic.jdbc.jts.viamd</jta-data-source>
<class>com.intiqua.viamd.model.datamanipulation.DataAccountingUnitBean</class>
<class>com.intiqua.viamd.model.datamanipulation.DataActionsBean</class>
<class>com.intiqua.viamd.model.datamanipulation.DataActionsRoleBean</class>
<class>com.intiqua.viamd.model.datamanipulation.DataAddressBean</class>
<class>com.intiqua.viamd.model.datamanipulation.DataAdmitOrderMatrixBean</class>
<class>com.intiqua.viamd.model.datamanipulation.DataAgeGroupBean</class>
<class>com.intiqua.viamd.model.datamanipulation.DataAgeGroupBrandBean</class>
<class>com.intiqua.viamd.model.datamanipulation.DataAOMatrixHospAssocBean</class>
<properties>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.logging.level.cache" value="FINEST" />
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.timestamp" value="true"/>
<property name="eclipselink.logging.session" value="true"/>
<property name="eclipselink.logging.exceptions" value="true"/>
<property name="eclipselink.logging.thread" value="true"/>
</properties>
</persistence-unit>
</persistence>
I am getting error while executing the below line of code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("viamd");
EntityManager em = emf.createEntityManager();
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.
Because my persistence.xml uses drop-and-create-tables:
<?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="USENETPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>net.bounceme.dur.usenet.model.NewsgroupsArticles</class>
<class>net.bounceme.dur.usenet.model.Article</class>
<class>net.bounceme.dur.usenet.model.Newsgroup</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/nntp"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="java"/>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
My triggers get removed each run. Where can I put the triggers so that they're not removed?
You should use a SessionCustomizer. And within your persistence properties you should insert a property pointing your SessionCustomizer implementation.
properties.put(PersistenceUnitProperties.SESSION_CUSTOMIZER, MySessionCustomizer.class.getName());
or
<property name="eclipselink.session.customizer" value="foo.bar.MySessionCustomizer" />
and within this session customizer you could load your trigger codes and let it be executed with:
unitOfWork.executeNonSelectingSQL(sql);
A detailed explanation regarding this topic you can find here
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.