I have a JavaFX Maven project in IntelliJ, which uses Hibernate. When the app is starting, I am getting the following error message:
No persistence provider for entitymanager named xyz
Why? My META-INF/persistence.xml is located in myproject/src/main/resources (the directory is checked as resource folder in project settings).
I am sure I've downloaded all Hibernate JARs via Maven.
My persistence.xml is correct:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="xyz">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entity.User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLiteDialect"/>
<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:java.sqlite"/>
<property name="javax.persistence.jdbc.driver_class" value="oracle.jdbc.driver"/>
<property name="hibernate.hbm2ddl.auto" value="none"/>
<property name="hibernate.show_sql" value="false"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
Java code:
factory = Persistence.createEntityManagerFactory("xyz");
In my an old non-Maven project the code above worked fine.
your persistence.xml must be located in the META-INF folder
There was a different context and solution in my case:
Context:
see the log below:
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:tcp://localhost/~/test]
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=sa}
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
...
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named NewPersistenceUnit`
Solution:
As its clear from logs above, persistenceUnit has been found and some data like dialect, username and password is extracted.
There was problem in mapping of entities. By commenting out #Entity one by one on entities, I was able to find the problem and solve it, without any change in persistence.xml file.
I have no idea why error like No Persistence provider for EntityManager named X should be shown when there is a problem in entity mapping!
Related
Im trying to configure a simple JPA project and I'm very stuck.
I have a working dataSource (MySQL) & GlassFish with multiple tables.
I did the following steps (all of the files generated by Intellij):
Create new project with Java EE Persistance with Hibernate Provider.
Import DB that has a Student table.
Generate Entity classes with Intellij IDE.
Import Hibernate-entitymanager library from Maven.
Import MySQL connector.
Try to run a simple JPA program to create new row but I get an exeption :
No Persistence provider for EntityManager named persistenceUnit
What can be the problem? I tried almost every tutorial and every question here but with no success!
Please Help me!
(sometimes the Hibernate in the persistance provider is colored red)
persistance:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="NewPersistenceUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>modules.CourseEntity</class>
<class>modules.StuCouEntity</class>
<class>modules.StudentsEntity</class>
<class>modules.TeacherEntity</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="123456"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Main.java:
import modules.StudentsEntity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class MainClass
{
public static void main(String[] args)
{
StudentsEntity stu = new StudentsEntity();
stu.setName("David");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
try
{
em.persist(stu);
em.getTransaction().commit();
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
ex.printStackTrace();
em.getTransaction().rollback();
}
finally
{
em.close();
}
}
}
error:
June 16, 2017 5:47:07 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
June 16, 2017 5:47:07 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.5.Final}
June 16, 2017 5:47:07 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
June 16, 2017 5:47:07 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named persistenceUnit
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:84)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at MainClass.main(MainClass.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
You defined
<persistence-unit name="NewPersistenceUnit">
while tryng to call Persistence.createEntityManagerFactory("persistenceUnit");
change NewPersistenceUnit to persistenceUnit
I have a problem with my hibernate.cfg.xml in IntelliJ IDE.
Here is my hibernate config file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver:class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost/HIndex</property>
<property name="hibernate.connection.username">index_user</property>
<property name="hibernate.connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL Dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</property>
<!-- Assume test is the database name -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the annotated entity class -->
<mapping class="HIndexSaar.HIndex.Person"/>
<mapping class="HIndexSaar.HIndex.University"/>
<mapping class="HIndexSaar.HIndex.Publication"/>
</session-factory>
</hibernate-configuration>
And this is my application class:
package HIndexSaar.HIndex;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateManager {
private static SessionFactory factory;
public HibernateManager(){
//
//* Setup the configuration.
//
Configuration config = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Person.class) .addAnnotatedClass(University.class).addAnnotatedClass(Publication.class);
factory = config.buildSessionFactory();
}
I get an error by running the following code:
package HIndexSaar.HIndex;
public class AppHibernate {
public static void main(String[] args){
HibernateManager mng = new HibernateManager();
[...]
}
}
So something seems to be broken in building the SessionFactory in the HibernateManager. I get the error
Could not locate cfg.xml resource
and the following stacktrace:
Feb 27, 2016 10:26:03 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.7.Final}
Feb 27, 2016 10:26:03 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 27, 2016 10:26:03 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:259)
at org.hibernate.cfg.Configuration.configure(Configuration.java:245)
at HIndexSaar.HIndex.HibernateManager.<init>(HibernateManager.java:18)
at HIndexSaar.HIndex.AppHibernate.main(AppHibernate.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
I already have placed the hibernate.cfg.xml file under /src/main/java/resources but obviously something goes still wrong.
Does somebody have an idea what could be the mistake I made? Surprisingly, the same code runs in Eclipse without error, so what could be the problem of IntelliJ? I use the same SDK/JDK, namely jdk 1.7.
If your are using intellij then try to create new module with hibernate framework support then paste this same code and execute it.
This might be path difference in clipse and intellij
I have hibernate project structure like this
My hibernate.cfg.xml is
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=WSMS</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<mapping class="test.IssuesEntity"/>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
My main class is
public class Main {
public static void main(String[] args) throws Exception {
/// System.out.print("jajaja");
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
//creating seession factory object
SessionFactory factory=cfg.buildSessionFactory();
//creating session object
Session session=factory.openSession();
//creating transaction object
Transaction t=session.beginTransaction();
IssuesEntity e1=new IssuesEntity();
// e1.setId(115);
// e1.setFirstName("sonoo");
// e1.setLastName("jaiswal");
e1.setIssueName("akash");
session.persist(e1);//persisting the object
t.commit();//transaction is commited
session.close();
System.out.println("successfully saved");
}
}
But whenever i run project it shows following error
Feb 26, 2016 10:02:59 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Feb 26, 2016 10:03:00 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.2.Final}
Feb 26, 2016 10:03:00 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 26, 2016 10:03:00 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
I have followed intellij instruction but i can not find any solution. Please help
It is not an error. It is just an information that you don't have hibernate.properties file in the class path. You have properties in the hibernate.cfg.xml, it is ok too. But you need to specify hibernate.dialect, hibernate.connection.username and hibernate.connection.password.
And if you don't have tables in the database you need this too
<property name="hbm2ddl.auto">update</property>
I'm trying to figure out why my Java program returns me "No persistence provider for EntityManager name WroProjectPU"
I'm thinking it's happening because Java can't find persistence.xml file.
I tried to use different configurations, but no one worked.
Currently, the structure of my program is looking like this:
where selected Main.java file is the file, where I have
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("WroProjectPU");
The main folder WroProject is under this absolute location:
C:\Users\Szymon\workspace\WroProject
Could you tell me where I should put my persistence.xml file? The full absolute path please.
Thanks in advance!
AFTER EDIT:
My persistence.xml looks like this:
<?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="WroProjectPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>db.User</class>
<class>db.Session</class>
<class>db.Email</class>
<class>db.File</class>
<class>db.FilePermission</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/wroproject"/>
<property name="javax.persistence.jdbc.password" value="postgres"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
</properties>
The problem:
lis 24, 2014 9:30:31 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
lis 24, 2014 9:30:31 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
lis 24, 2014 9:30:31 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named WroProjectPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at wroproject.Main.main(Main.java:18)
Java Result: 1
we are integrating the java blazeds hibernate with flex project..First we tested the pure java hibernate in eclipse and it is executing fine. But when we put the same in tomcat for flex integration with blazeds it is showing the following error.this is the only error.
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.6
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
Jan 24, 2013 11:31:31 AM org.hibernate.cfg.Configuration getConfigurationInputSt
ream
INFO: Configuration resource: /hibernate.cfg.xml
this is the hibernate.cfg.xml(To identify whether hibernate.cfg.xml is not found and i tested by deleting DOCTYPE of .cfg.xml then tomcat output displayed saying the root element is not found...which means it is able to find the hibernate.cfg.xml( i think)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<mapping class="com.model.User" />
<mapping class="com.model.UserDetails" />
<mapping class="com.model.LoanDetails" />
<mapping class="com.model.BorrowerDetails" />
</session-factory>
</hibernate-configuration>
this is hiberutil.java
public class HibernateUtil
{
private static SessionFactory sessionFactory=configureSessionFactory();
private static ServiceRegistry serviceRegistry;
private static SessionFactory configureSessionFactory() throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
why the error is showing? the control is coming from flex side and executing the starting point of the java method...but when it comes to hibernate stuff execution, it is showing this error in tomcat...
But when i execute java+hibernate as a pure java application it is executing fine..
can anybody help me?
Thanks
EDIT 1 After the modification suggested by #Andremoniy and putting the .cfg.xml in src folder and here also in eclipse it is working but not in tomcat with flex
following error comes
org.hibernate.MappingException: An AnnotationConfiguration instance is required
to use <mapping class="com.model.User"/>
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.jav
a:1606)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.jav
a:1561)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1540)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1514)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1434)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
at com.sample.HibernateUtil.configureSessionFactory(HibernateUtil
.java:26)
at com.sample.HibernateUtil.<clinit>(HibernateUtil.java:20)
at com.sample.App.checkUser(App.java:34)
I guess the problem is with the code for configuration you mentioned for .hbm.xml may not be working in Tomcat.
I think it needs AnnotationConfiguration object. I guess you used this code due to the Annotationconfiguration object creation is not working.
Better create a maven hibernate project with pom and export the war file to Tomcat (with the changes of Annotationconfiguration). Also use log4j jar for showing the details of Tomcat execution with the debug outputs, so that Tomcat output will print all of execution flow.
It seems to me, that you can solve this problem, by passing hibernate.cfg.xml directly into Configuration.
try (InputStream in = HibernateUtil.class.getResourceAsStream("/hibernate.cfg.xml")) {
Configuration configuration = new Configuration().addInputStream(in).configure();
...
} ...
The only thing, you have to properly point the path or class, for getResourceAsStream. Or you may be want to create InputStream using some relative path.
I have faced the same problem but I found the solution here..
http://www.mkyong.com/hibernate/hibernate-error-an-annotationconfiguration-instance-is-required-to-use/
ie. Change the line:
new Configuration().configure().buildSessionFactory();
to:
new AnnotationConfiguration().configure().buildSessionFactory();
if it is appeared that you use Intellij Idea you need to put hibernate.cfg.xml file into resources folder
and if you will have after next error:
org.hibernate.service.jndi.JndiException: Error parsing JNDI name []
delete in hibernate.cfg.xml file name of tag see picture: