我是JPA&冬眠。在我的网络应用程序中,我已将我的JDBC代码更改为JPA。在运行网络应用程序时,我得到了一个很大的错误列表。但是根据我对JPA和Hibernate的了解,我认为下面的两个错误代表了我的大多数问题。
我在网上搜索了很多这些错误。但我找不到一个解决方案。我已经包含了所有必需的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)
<?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:
我有一些基本问题:
如果使用JPA注释,JPA + Hibernate组合中涉及多少个xml文件? 我只有persistence.xml。但总共3(dwr.xml&amp; web.xml)
如果我使用JPA注释,是否需要hibernate.cfg.xml。因为,直到现在我还没有添加它。
如果使用JPA 2.0&amp ;;你是否应该给我基本的JAR文件名列表? Hibernate的!因为,我有超过15个文件。
答案 0 :(得分:6)
您正在使用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映射文件。
感谢您的回复,对不起orm.xml。我强烈要求显示persistence.xml的位置。我没有使用XML映射。该orm.xml是自动生成的。要删除那个文件吗?
我会尝试删除orm.xml
,entity-mappings
是orm.xml
的根元素,这就是错误所在。
“如果您使用的是JPA 2.0提供商”,您的意思是什么?提供者意味着,Hibernate ??
JPA只是一个API。要使用JPA,您需要实现此API,此类实现称为持久性提供程序(EcliseLink,Hibernate,OpenJPA)。在您的情况下,您正在使用Hibernate EntityManager 3.4.0.GA,这是一个JPA 1.0实现。
我需要做些什么来改用JPA 2.0?通过添加javax.persistence_2.0.0.jar或通过更改使用JPA 2.0的hibernate?
两者都必须使用Hibernate EntityManager 3.5+(及其依赖项,包括javax.persistence_2.0.0.jar)。
答案 1 :(得分:0)
16047 [31149935 @ qtp-23671010-1]错误 org.hibernate.util.XMLHelper - 错误 解析XML:XML InputStream(2) cvc-complex-type.3.1:值'2.0' 属性'''的元素 'entity-mappings'无效 尊重相应的属性 使用。属性“版本”有一个固定的 价值'1.0'。
使用某种属性版本的值看起来这是xml中的错误。