hibernate - JPA Error- No Persistence provider for EntityManager named -
i can't solve problem @ , there ton of stack overflow posts exact same error message. have tried them , still can't work. maybe on looking small. need , thank in advance.
error message
initial entitymanagerfactory creation failed.javax.persistence.persistenceexception: no persistence provider entitymanager named org.hibernate.ejb.hibernatepersistence exception in thread "main" java.lang.exceptionininitializererror @ util.databaseutil.buildsessionfactory(databaseutil.java:18) @ util.databaseutil.<clinit>(databaseutil.java:8) @ test.main(test.java:68) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ com.intellij.rt.execution.application.appmain.main(appmain.java:144) caused by: javax.persistence.persistenceexception: no persistence provider entitymanager named org.hibernate.ejb.hibernatepersistence @ javax.persistence.persistence.createentitymanagerfactory(persistence.java:61) @ javax.persistence.persistence.createentitymanagerfactory(persistence.java:39) @ util.databaseutil.buildsessionfactory(databaseutil.java:13) ... 7 more
directory structure please ignore hibernate.cfg.xml file, tried using hibernate sessionfactory , ran major error couldn't solve , said hell it. plus post says jpa preferred
here persistence.xml file
<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="com.learning.jpa"> <provider>org.hibernate.jpa.hibernatepersistenceprovider</provider> <class>com.learning.jpa.entities.user</class> <class>com.learning.jpa.entities.expense</class> <class>com.learning.jpa.entities.category</class> <properties> <property name="hibernate.connection.pool_size" value="1"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.driver"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/budget_calculator"/> <property name="hibernate.onnection.username" value="root"/> <property name="hibernate.connection.password" value="password"/> <property name="hibernate.dialect" value="org.hibernate.dialect.mysqldialect"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.hbm2ddl.auto" value="update"/> </properties> </persistence-unit> </persistence>
entitymanagerfactory
public class databaseutil { private static entitymanagerfactory sessionfactory = buildsessionfactory(); private static entitymanagerfactory buildsessionfactory() { try { if (sessionfactory == null) { sessionfactory = persistence.createentitymanagerfactory("org.hibernate.ejb.hibernatepersistence"); } return sessionfactory; } catch (throwable ex) { system.err.println("initial entitymanagerfactory creation failed." + ex); throw new exceptionininitializererror(ex); } } public static entitymanagerfactory getsessionfactory() { return sessionfactory; } public static void shutdown() { getsessionfactory().close(); } }
you using wrong identifier persistence-unit. name of specified here:
... <persistence-unit name="com.learning.jpa"> ... </persistence-unit> </persistence>
the stacktrace tells
no persistence provider entitymanager named org.hibernate.ejb.hibernatepersistence
so need reference pu via it's correct, specified name com.learning.jpa in relevant code snippet so:
if (sessionfactory == null) { sessionfactory = persistence.createentitymanagerfactory("com.learning.jpa"); }
having changed correct pu-name should work. hope helps.
Comments
Post a Comment