javax.persistence.PersistenceException:没有名为hibernate.cfg.xml

时间:2016-07-15 06:27:20

标签: hibernate jpa persistence-provider

我正在尝试使用Intellij Idea上的Hibernate连接数据库。

Bean文件是基于Hibernate实体类自动生成的。

public class SirketEntityManagedBean {

    private EntityManagerFactory myEntityManagerFactory;

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

    // ...
}

当我运行它时,会抛出以下异常:

  

javax.persistence.PersistenceException:没有名为hibernate.cfg.xml的EntityManager的持久性提供程序   javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)   javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)   beans.SirketEntityManagedBean<初始化>(SirketEntityManagedBean.java:40)   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)   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)

这是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>

这是项目目录:

maven_dir

这是如何引起的?如何解决?

3 个答案:

答案 0 :(得分:3)

createEntityManagerFactory方法的名称为persistence-unit。某些Hibernate配置文件( hibernate.cfg.xml )不是persistence-unit的名称。在文件 persistence.xml 中指定了persistence-unit。这将在任何JPA文档中充分描述,您显然没有阅读

答案 1 :(得分:1)

createEntityManagerFactory方法将persistent unit作为参数。持久性单元在名为persistence.xml的文件中描述,此文件应位于项目的类路径中

例如,如果您的持久单元名称是&#34; exampledb&#34;,那么方法调用将是 -

  

EntityManagerFactory exDb =   Persistence.createEntityManagerFactory(&#34; exampledb&#34);

请参阅this问题,另请参阅persistence.xml的示例

答案 2 :(得分:0)

可能是因为bean的定义。您是否在xml配置文件中定义了bean定义?如果您这样做,请检查配置文件中的类名称。如果您没有,请在课程定义的顶部添加@ManagedBean(name = "sirketEntity")

像这样:

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