Java 无法配置 EntityManagerFactory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3744761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:15:41  来源:igfitidea点击:

Unable to configure EntityManagerFactory

javahibernatejpa

提问by

I am new to JPA & hibernate. In my web-app i have changed my JDBC codes to JPA. While running the web-app i am getting a BIG list of errors. But from my knowledge in JPA and Hibernate, I think the two errors below represent most of my problem.

我是 JPA 和休眠的新手。在我的网络应用程序中,我已将 JDBC 代码更改为 JPA。在运行网络应用程序时,我收到了一大堆错误。但是根据我对 JPA 和 Hibernate 的了解,我认为下面的两个错误代表了我的大部分问题。

  • Unable to configure EntityManagerFactory
  • 16047 [31149935@qtp-23671010-1] ERROR org.hibernate.util.XMLHelper - Error parsing XML: XML InputStream(2) cvc-complex-type.3.1: Value '2.0' of attribute 'version' of element 'entity-mappings' is not valid with respect to the corresponding attribute use. Attribute 'version' has a fixed value of '1.0'.
  • 无法配置 EntityManagerFactory
  • 16047 [31149935@qtp-23671010-1] 错误 org.hibernate.util.XMLHelper - 解析 XML 时出错:XML InputStream(2) cvc-complex-type.3.1:元素“实体”的属性“版本”的值“2.0” mappings' 对于相应的属性使用无效。属性“version”具有固定值“1.0”。

I have searched a lot for these errors on the web. But I can't find one solution. I have included all the required JARs and added persistence.xml to the class path. I am not able to find the reason.

我在网上搜索了很多这些错误。但我找不到一种解决方案。我已经包含了所有必需的 JAR 并将persistence.xml 添加到类路径中。我无法找到原因。

Here my full list of console errors:

这是我的控制台错误的完整列表:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Documents%20and%20Settings/tamilvendhank/Local%20Settings/Temp/Jetty_127_0_0_    1_8080_ExpMgmtWeb.war__ExpMgmtWeb__w96xvk_3923622842201679764/webapp/WEB-INF/lib/slf4j-log4j12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Documents%20and%20Settings/tamilvendhank/Local%20Settings/Temp/Jetty_127_0_0_1_8080_ExpMgmtWeb.war__ExpMgmtWeb__w96xvk_3923622842201679764/webapp/WEB-INF/lib/slf4j-simple-1.5.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
15360 [31149935@qtp-23671010-1] INFO  org.hibernate.cfg.annotations.Version  -    Hibernate Annotations 3.4.0.CR1
15375 [31149935@qtp-23671010-1] INFO  org.hibernate.cfg.Environment  - Hibernate 3.3.0.CR1
15375 [31149935@qtp-23671010-1] INFO  org.hibernate.cfg.Environment  - hibernate.properties not found
15375 [31149935@qtp-23671010-1] INFO  org.hibernate.cfg.Environment  - Bytecode provider name : cglib
15375 [31149935@qtp-23671010-1] INFO  org.hibernate.cfg.Environment  - using JDK 1.4 java.sql.Timestamp handling
15469 [31149935@qtp-23671010-1] INFO  org.hibernate.annotations.common.Version  - Hibernate Commons Annotations 3.1.0.CR1
15469 [31149935@qtp-23671010-1] INFO  org.hibernate.ejb.Version  - Hibernate EntityManager 3.4.0.CR1
16047 [31149935@qtp-23671010-1] ERROR org.hibernate.util.XMLHelper  - Error parsing XML: XML InputStream(2) cvc-complex-type.3.1: Value '2.0' of attribute 'version' of element 'entity-mappings' is not valid with respect to the corresponding attribute use. Attribute 'version' has a fixed value of '1.0'.
2010-09-19 11:23:40.265:WARN::Error for /ExpMgmtWeb/dwr/call/plaincall/ExpenseDetailsManagement.getexpenseList.dwr
java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.directwebremoting.create.NewCreator.getInstance(NewCreator.java:66)
at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:344)
at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:279)
at org.directwebremoting.servlet.PlainCallHandler.handle(PlainCallHandler.java:52)
at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:101)
at org.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:144)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
2010-09-19 11:23:40.265:WARN::Nested in java.lang.ExceptionInInitializerError:
javax.persistence.PersistenceException: [PersistenceUnit: ExpensePersistentUnit] Unable to configure EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:265)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at com.pricar.JPAInteg.ExpenseDetailsManagement.<clinit>(ExpenseDetailsManagement.java:21)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.directwebremoting.create.NewCreator.getInstance(NewCreator.java:66)
at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:344)
at org.directwebremoting.impl.DefaultRemoter.execute(DefaultRemoter.java:279)
at org.directwebremoting.servlet.PlainCallHandler.handle(PlainCallHandler.java:52)
at org.directwebremoting.servlet.UrlProcessor.handle(UrlProcessor.java:101)
at org.directwebremoting.servlet.DwrServlet.doPost(DwrServlet.java:144)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

alt text

替代文字

 <?xml version="1.0" encoding="UTF-8"?>
 <persistence>
<persistence-unit name="ExpensePersistentUnit">
 <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.pricar.JPAInteg.Role</class>
    <class>com.pricar.JPAInteg.User</class>
    <class>com.pricar.JPAInteg.Userdetail</class>
    <class>com.pricar.JPAInteg.Category</class>
    <class>com.pricar.JPAInteg.Expens</class>
    <class>com.pricar.JPAInteg.Leavetable</class>
    <class>com.pricar.JPAInteg.Permissiontoken</class>
    <class>com.pricar.JPAInteg.Roletokenassociation</class>
    <class>com.pricar.JPAInteg.UserPK</class>
 <properties>
    <property name="hibernate.connection.url" value="jdbc:mysql://localhost/officemgmt"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
    <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
    <property name="hibernate.connection.password" value="1234"/>
    <property name="hibernate.connection.username" value="root"/>
    <property name="hibernate.hbm2ddl.auto" value="update"/>
    <property name="hibernate.show_sql" value="true"/>
 </properties>
 </persistence-unit> 
 </persistence>


Update:

更新:

javax.persistence.PersistenceException: [PersistenceUnit: ExpensePersistentUnit] Unable to configure EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:371)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at com.pricar.JPAInteg.ExpenseDetailsManagement.<clinit>(ExpenseDetailsManagement.java:21)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)


Update 2:

更新 2:

I have some basic questions:

我有一些基本问题:

  1. How many xml files involved in JPA+Hibernate combination, if JPA annotations were used? i am having just persistence.xml. But totally 3(dwr.xml & web.xml)

  2. Is hibernate.cfg.xml needed, if i use JPA annotaions. Because, i didnt added it till now.

  3. Shall you give me the list of basic JAR file names, in case of using JPA 2.0 & Hibernate!!! Because, i am having more than 15 files.

  1. 如果使用JPA注解,JPA+Hibernate组合涉及多少个xml文件?我只有persistence.xml。但总共 3 个(dwr.xml & web.xml)

  2. 如果我使用 JPA annotaions,是否需要 hibernate.cfg.xml。因为,我直到现在才添加它。

  3. 如果使用 JPA 2.0 和 Hibernate,请给我基本 JAR 文件名的列表!!!因为,我有超过 15 个文件。

采纳答案by Pascal Thivent

You are using a JPA 1.0 implementation, you need to provide a JPA 1.0 compliant orm.xml(note the version attribute amongst other things):

您正在使用 JPA 1.0 实现,您需要提供符合 JPA 1.0 的 orm.xml(注意版本属性等):

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings 
  xmlns="http://java.sun.com/xml/ns/persistence/orm"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
  version="1.0">
  ...
</entity-mappings>

Yours probably looks like:

你的可能看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
  xmlns="http://java.sun.com/xml/ns/persistence/orm"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
  version="2.0">
  ...
</entity-mappings>

Which is correct... ifyou are using a JPA 2.0 provider.

哪个是正确的...如果您使用的是 JPA 2.0 提供程序。

But to be honest, I don't know why you are providing an orm.xml, you usually don't need an XML mapping file when using annotations.

但说实话,我不知道你为什么要提供一个orm.xml,你在使用注解时通常不需要 XML 映射文件。

References

参考

  • JPA 1.0 Specifciation
    • Section "10.2 XML Schema"
  • JPA 2.0 Specifciation
    • Section "12.3 XML Schema"
  • JPA 1.0 规范
    • “10.2 XML 模式”部分
  • JPA 2.0 规范
    • “12.3 XML 模式”部分


Thanks for your reply and sorry for the orm.xml. My intense is to show the location of persistence.xml. I am not using XML mapping. That orm.xml is auto generated. Shall I remove that file?

感谢您的回复,并为 orm.xml 感到抱歉。我的重点是显示persistence.xml的位置。我没有使用 XML 映射。该 orm.xml 是自动生成的。我要删除那个文件吗?

I would try to remove the orm.xmlindeed, entity-mappingsis the root element of the orm.xml, that's what the error is about.

我会尝试删除orm.xml确实entity-mappings是 的根元素orm.xml,这就是错误的原因。

What do you mean in "if you are using a JPA 2.0 provider"? Provider means, Hibernate??

“如果您使用的是 JPA 2.0 提供程序”是什么意思?Provider 的意思是,Hibernate??

JPA is just an API. To use JPA, you need an implementation of this API and such implementations are called persistence providers (EcliseLink, Hibernate, OpenJPA). In your case, you are using Hibernate EntityManager 3.4.0.GA which is a JPA 1.0 implementation.

JPA 只是一个 API。要使用 JPA,您需要实现此 API,此类实现称为持久性提供程序(EcliseLink、Hibernate、OpenJPA)。在您的情况下,您使用的是 Hibernate EntityManager 3.4.0.GA,它是一个 JPA 1.0 实现。

What I have to do to change to JPA 2.0? by adding javax.persistence_2.0.0.jar or by change the hibernate which is using JPA 2.0??

我必须做什么才能更改为 JPA 2.0?通过添加 javax.persistence_2.0.0.jar 或更改使用 JPA 2.0 的休眠?

Both, you'd have to use Hibernate EntityManager 3.5+ (and its dependencies, and this includes the javax.persistence_2.0.0.jar).

两者都必须使用 Hibernate EntityManager 3.5+(及其依赖项,包括 javax.persistence_2.0.0.jar)。

回答by Jaydeep Patel

16047 [31149935@qtp-23671010-1] ERROR org.hibernate.util.XMLHelper - Error parsing XML: XML InputStream(2) cvc-complex-type.3.1: Value '2.0' of attribute 'version' of element 'entity-mappings' is not valid with respect to the corresponding attribute use. Attribute 'version' has a fixed value of '1.0'.

16047 [31149935@qtp-23671010-1] 错误 org.hibernate.util.XMLHelper - 解析 XML 时出错:XML InputStream(2) cvc-complex-type.3.1:元素“实体”的属性“版本”的值“2.0” mappings' 对于相应的属性使用无效。属性“version”具有固定值“1.0”。

Looks like this is error in your xml with some kind of attribute version's value.

看起来这是您的 xml 中的错误,具有某种属性版本的值。