1

I am trying to connect database using Hibernate on Intellij Idea.

Bean file is auto-generated based on Hibernate entity class.

public class SirketEntityManagedBean {

    private EntityManagerFactory myEntityManagerFactory;

    public SirketEntityManagedBean() {
        myEntityManagerFactory = Persistence.createEntityManagerFactory("hibernate.cfg.xml");
    }

    // ...
}

When I run it, below exception is thrown:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named hibernate.cfg.xml javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69) javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) beans.SirketEntityManagedBean.<init>(SirketEntityManagedBean.java:40) sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) java.lang.reflect.Constructor.newInstance(Constructor.java:423) java.lang.Class.newInstance(Class.java:442) com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:186) com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:100) com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409) com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244) com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116) com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:71) org.apache.el.parser.AstValue.getValue(AstValue.java:161) org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184) com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) org.primefaces.component.panelmenu.PanelMenu.getModel(PanelMenu.java:84) org.primefaces.component.menu.BaseMenuRenderer.encodeEnd(BaseMenuRenderer.java:108) javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1903) javax.faces.render.Renderer.encodeChildren(Renderer.java:176) javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889) org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:84) org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:71) org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:49) javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1903) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899) javax.faces.component.UIComponent.encodeAll(UIComponent.java:1899) com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:451) com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Here is persistence.xml:

<?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>hib.OrtamEntity</class>
        <class>hib.SorumluEntity</class>
        <class>hib.SirketEntity</class>
        <class>hib.VpnkullaniciEntity</class>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@//XXX.X.XX.XX:1521/PROD"/>
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
            <property name="hibernate.connection.username" value="XXTEST"/>
            <property name="hibernate.connection.password" value="XXTEST"/>
            <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>

And this is project directory:

maven_dir

How is this caused and how can I solve it?

Erdel
  • 370
  • 3
  • 16
  • If the init is not called, how is it PrimeFaces related then? Any errors in the logging? What if you run the app in 'development mode' where do you fine that it is a bean? faces-config.xml? Please create an [mcve] – Kukeltje Jul 15 '16 at 06:41
  • So what part of that stacktrace is not clear? Your title is wrong, your tagging was wrong (but 'wrongly corrected') – Kukeltje Jul 15 '16 at 06:49
  • Do you have persistence.xml? I strongly feel that your persistent unit name is not "hibernate.cfg.xml". Your configuration is incorrect. Please post the persistence.xml here, if you have one. Make sure the persistence.xml is your classpath too. You can refer to this question to get more idea - http://stackoverflow.com/questions/1158159/no-persistence-provider-for-entitymanager-named – Manish Jul 15 '16 at 07:01
  • I have edited question @Manish – Erdel Jul 15 '16 at 07:10
  • @Erdinc - I posted an answer to this question. You need to change to createEntityManagerFactory("NewPersistenceUnit") – Manish Jul 15 '16 at 07:13
  • @Manish did not work. Same error. – Erdel Jul 15 '16 at 07:20
  • @Erdinç - Make sure the persistence.xml is in the classpath. – Manish Jul 15 '16 at 07:24
  • @Erdinç - Can you please check if the persistence.xml is in this directory /WEB-INF/classes/META-INF/ – Manish Jul 15 '16 at 07:29
  • @Manish This time i get >javax.servlet.ServletException: Could not initialize class org.hibernate.ejb.Ejb3Configuration javax.faces.webapp.FacesServlet.service(FacesServlet.java:659) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) – Erdel Jul 15 '16 at 07:53
  • @Manish I think it is related with hibernate-annotations jar. I have manually added jar but it did not solve the problem. – Erdel Jul 15 '16 at 07:54
  • @Erdinç You seem to be right about hibernate-annotations jar. But this issue is not relevant to the question which you posted. You might want to post another question for this issue. Please upvote the answer if it solved your original issue. – Manish Jul 15 '16 at 08:00
  • You clearly do NOT need some hibernate.cfg.xml file when using JPA (and persistence.xml) –  Jul 15 '16 at 10:45

3 Answers3

3

The createEntityManagerFactory method takes in the name of a persistence-unit. Some Hibernate config file (hibernate.cfg.xml) is not the name of a persistence-unit. A persistence-unit is specified in file persistence.xml. This would be adequately described in any JPA documentation, which you evidently haven't read

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
1

The createEntityManagerFactory method takes in the persistent unit as argument. The persistent unit is described in a file named persistence.xml and this file should be in the classpath of your project.

For example if your persistent unit name is "exampledb", then the method call would be -

EntityManagerFactory exDb = Persistence.createEntityManagerFactory("exampledb");

Please refer this question too see an example of persistence.xml

Community
  • 1
  • 1
Manish
  • 710
  • 3
  • 12
  • 25
0

It might be because of the bean definitions. Did you define bean definitions on xml config file? If you did, check the class names on the config file. If you didn't, add @ManagedBean(name = "sirketEntity") on the top of you class definition.

Like this:

@ManagedBean(name = "sirketEntity")
public class SirketEntityManagedBean {