1
votes

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?

3
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 minimal reproducible example - Kukeltje
So what part of that stacktrace is not clear? Your title is wrong, your tagging was wrong (but 'wrongly corrected') - Kukeltje
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 - stackoverflow.com/questions/1158159/… - Manish
I have edited question @Manish - Erdinç
@Erdinc - I posted an answer to this question. You need to change to createEntityManagerFactory("NewPersistenceUnit") - Manish

3 Answers

3
votes

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

1
votes

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

0
votes

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 {