gpt4 book ai didi

java - 无法配置 EntityManagerFactory

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:26:32 25 4
gpt4 key购买 nike

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

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

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

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

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>

更新:

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)

更新 2:

我有一些基本问题:

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

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

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

最佳答案

您正在使用 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>

你的可能看起来像:

<?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>

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

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

引用资料

  • 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 确实,entity-mappingsorm.xml 的根元素,就是这样错误是关于。

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

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??

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

关于java - 无法配置 EntityManagerFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3744761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com