休眠 JPA 错误
Posted
技术标签:
【中文标题】休眠 JPA 错误【英文标题】:Hibernate JPA Error 【发布时间】:2012-04-19 00:30:37 【问题描述】:我一直在尝试在 Spring 3.0 项目中使用 Hibernate+JPA 从数据库中获取数据。它给了我异常 java.lang.NoSuchFieldError: INSTANCE。在这里,我给出了完整的堆栈跟踪。
java.lang.NoSuchFieldError: INSTANCE
at org.hibernate.type.BasicTypeRegistry.<init>(BasicTypeRegistry.java:94)
at org.hibernate.type.TypeResolver.<init>(TypeResolver.java:59)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:250)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:302)
at org.hibernate.cfg.AnnotationConfiguration.<init>(AnnotationConfiguration.java:98)
at org.hibernate.ejb.Ejb3Configuration.<clinit>(Ejb3Configuration.java:133)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at com.mpm.login.service.UserAuthentication.authenticateUser(UserAuthentication.java:30)
at com.mpm.login.controller.LoginController.authenticate(LoginController.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:174)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:84)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
我的依赖树如下所示:
[INFO] -----------------------------------------------------------------------
[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ Login ---
[INFO] Login:Login:war:0.0.1-SNAPSHOT
[INFO] +- org.springframework:spring-web:jar:3.0.0.RELEASE:compile
[INFO] | +- aopalliance:aopalliance:jar:1.0:compile
[INFO] | +- org.springframework:spring-beans:jar:3.0.0.RELEASE:compile
[INFO] | +- org.springframework:spring-context:jar:3.0.0.RELEASE:compile
[INFO] | | \- org.springframework:spring-expression:jar:3.0.0.RELEASE:compil
[INFO] | \- org.springframework:spring-core:jar:3.0.0.RELEASE:compile
[INFO] +- log4j:log4j:jar:1.2.16:compile
[INFO] +- org.springframework:spring-webmvc:jar:3.0.0.RELEASE:compile
[INFO] | \- org.springframework:spring-asm:jar:3.0.0.RELEASE:compile
[INFO] +- org.springframework:spring-orm:jar:3.0.3.RELEASE:compile
[INFO] | +- org.springframework:spring-jdbc:jar:3.0.3.RELEASE:compile
[INFO] | \- org.springframework:spring-tx:jar:3.0.3.RELEASE:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:3.6.9.Final:compile
[INFO] | +- org.hibernate:hibernate-core:jar:3.6.9.Final:compile
[INFO] | | +- antlr:antlr:jar:2.7.6:compile
[INFO] | | +- commons-collections:commons-collections:jar:3.1:compile
[INFO] | | +- org.hibernate:hibernate-commons-annotations:jar:3.2.0.Final:co
ile
[INFO] | | \- javax.transaction:jta:jar:1.1:compile
[INFO] | \- javassist:javassist:jar:3.12.0.GA:compile
[INFO] +- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:jar:1.0.0.Fina
compile
[INFO] +- junit:junit:jar:4.8.1:compile
[INFO] +- org.springframework:spring-test:jar:3.0.3.RELEASE:compile
[INFO] +- mysql:mysql-connector-java:jar:5.1.17:compile
[INFO] +- org.springframework.ws:spring-ws-core:jar:2.0.2.RELEASE:compile
[INFO] | +- org.springframework.ws:spring-xml:jar:2.0.2.RELEASE:compile
[INFO] | +- org.springframework:spring-aop:jar:3.0.5.RELEASE:compile
[INFO] | +- org.springframework:spring-oxm:jar:3.0.5.RELEASE:compile
[INFO] | +- wsdl4j:wsdl4j:jar:1.6.1:compile
[INFO] | +- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO] | \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- jaxen:jaxen:jar:1.1:compile
[INFO] | +- dom4j:dom4j:jar:1.6.1:compile
[INFO] | +- xml-apis:xml-apis:jar:1.3.02:compile
[INFO] | +- xerces:xercesImpl:jar:2.6.2:compile
[INFO] | \- xom:xom:jar:1.0:compile
[INFO] | +- xerces:xmlParserAPIs:jar:2.6.2:compile
[INFO] | +- xalan:xalan:jar:2.6.0:compile
[INFO] | \- com.ibm.icu:icu4j:jar:2.6.1:compile
[INFO] +- javax.mail:mail:jar:1.4:compile
[INFO] | \- javax.activation:activation:jar:1.1:compile
[INFO] +- javax.servlet:servlet-api:jar:2.4:provided
[INFO] +- javax.servlet.jsp:jsp-api:jar:2.0:provided
[INFO] +- org.slf4j:slf4j-api:jar:1.6.4:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.6.4:compile
[INFO] +- jdom:jdom:jar:1.0:compile
[INFO] \- javax.persistence:persistence-api:jar:1.0-rev-1:compile
[INFO] ----------------------------------------------------------------------
LoginConroller.java
@控制器 公共类登录控制器
@RequestMapping(value = "login.action", method = RequestMethod.POST) public @ResponseBody HashMap authenticate(@RequestParam("username") String username, @RequestParam("password") 字符串密码,HttpServletRequest 请求) HashMap loginMap = new HashMap();
System.out.println("HomeController: Passing through...");
System.out.println("uname "+ username + "pass "+password);
Login userLogin = new Login();
userLogin.setUsername(username);
userLogin.setPassword(password);
//Conectivity to JPA
System.out.println("IP Address:"+request.getRemoteAddr());
System.out.println("HOST:"+request.getRemoteHost());
System.out.println("Port:"+request.getRemotePort());
System.out.println("User:"+request.getRemoteUser());
System.out.println("Browser:"+request.getHeader("User-Agent"));
/*
* Write the code to audit the login detail in login audit table
*/
UserAuthentication userAuthentication = new UserAuthentication();
boolean isUserAuthenticated=userAuthentication.authenticateUser(username, password);
System.out.println("auth is "+isUserAuthenticated);
userLogin.setIsUserAuthenticated(isUserAuthenticated?"true":"false");
// req.setAttribute("status", isUserAuthenticated);
// loginMap.put("data", userLogin);
loginMap.put("success", isUserAuthenticated);
//request.setAttribute
//System.out.println("----"+ login.u);
System.out.println("----");
// System.out.println("final result is... " + userAuthenticate);
return loginMap;
//return "redirect:success.jsp";
// return "success:true";
UserAuthentication.java
公共类 UserAuthentication
/**
* @param args
* Authenticates User from database and returns true
* if user is authenticated
*/
public boolean authenticateUser(String uname, String password)
// TODO Auto-generated method stub
System.out.println(" Inside authenticateUser");
boolean userAuthenticate=false;
List lst=null;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("DBConnection");
EntityManager em = emf.createEntityManager();
System.out.println(" Before Try block");
try
em.getTransaction().begin();
//Select all the record from Login table
Query query = em.createQuery("SELECT l FROM Login l where l.username = '"+uname+"' and l.password='"+password+"'");
System.out.println("query = " + query);
lst = query.getResultList();
System.out.println("lst = "+lst.size());
if(lst.size()>0)
userAuthenticate=true;
/*Login login = (Login) it.next();
System.out.print("Id:"+login.getId());
System.out.print(" Name:"+login.getUname());
System.out.println(" Password:"+login.getPassword());*/
em.getTransaction().commit();
catch(Exception e)
System.out.println("in catch");
System.out.println(e.getMessage());
finally
em.close();
return userAuthenticate;
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<persistence-unit name="DBConnection">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.mpm.login.model.Login</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/project"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<!--
<property name="hibernate.format_sql" value="true"/>
-->
</properties>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Login</groupId>
<artifactId>Login</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Login</name>
<url>http://maven.apache.org</url>
<build>
<finalName>Login</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
<properties>
<org.springframework.version>3.0.6.RELEASE</org.springframework.version>
<org.hibernate.version>3.3.1.GA</org.hibernate.version>
<org.springframework.test.version>2.5</org.springframework.test.version>
<spring.version>3.0.0.RELEASE</spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>$spring.version</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.9.Final</version>
<exclusions>
<exclusion>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</exclusion>
<exclusion>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<dependency>
<groupId>org.springframework.ws</groupId>
<artifactId>spring-ws-core</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<!--Adding the Servlet 2.4 Specification as a Dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<!-- Adding the JSP 2.0 Specification as a Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- Adding the logging Dependency -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0-rev-1</version>
</dependency>
<dependency>
<groupId>jboss</groupId>
<artifactId>jboss-common</artifactId>
<version>4.0.2</version>
</dependency>
<dependency>
<groupId>jboss</groupId>
<artifactId>jboss-cache</artifactId>
<version>1.4.1.GA</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>JBoss</id>
<name>JBoss repository</name>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss</url>
</repository>
</repositories>
</project>
谁能帮助解决这个问题?此错误的原因可能是什么以及如何解决?
谢谢 卡皮尔
例外:
java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.ejb.Ejb3Configuration
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at com.mpm.login.service.UserAuthentication.authenticateUser(UserAuthentication.java:30)
at com.mpm.login.controller.LoginController.authenticate(LoginController.java:50)
【问题讨论】:
信息不够,至少你应该提供com.mpm.login.service.UserAuthentication.authenticateUser()
和com.mpm.login.controller.LoginController.authenticate()
的代码。
嗨,Ken,请看下面的代码。 public boolean authenticateUser(String uname, String password) // TODO 自动生成的方法 stub System.out.println("Inside authenticateUser");布尔用户身份验证=假;列表 lst=null; EntityManagerFactory emf = Persistence.createEntityManagerFactory("DBConnection"); EntityManager em = emf.createEntityManager(); System.out.println("Before Try 块");
@RequestMapping(value = "login.action", method = RequestMethod.POST) public @ResponseBody HashMap authenticate(@RequestParam("username") String username, @RequestParam("password") String password , HttpServletRequest 请求) HashMap loginMap = new HashMap(); UserAuthentication userAuthentication = new UserAuthentication(); boolean isUserAuthenticated=userAuthentication.authenticateUser(用户名,密码); System.out.println("auth is "+isUserAuthenticated);
请勿将代码放在评论中,请通过编辑您的帖子发布它们
Ken,我已经在上面发布了我的 pom.xml。我一直在寻找其他版本的hibernate jar文件,但找不到。
【参考方案1】:
检查您的“com.mpm.login.model.Login”文件是否有任何声明为INSTANCE
的变量,如果存在则无法将该变量映射到数据库列。
【讨论】:
在提到的类中没有任何变量声明为实例。 INSTANCE 变量在 BasicTypeRegistry.java 中,它是一个休眠库文件。那么还有其他线索吗? 我正在为我的 Hibernate 和 OpenJPA 项目使用以下库文件 但是我没有使用 EJB。 dom4j-1.6.1.jar、hibernate-annotations-3.4.0.GA.jar、hibernate-commons-annotations-3.1.0.GA.jar、hibernate-entitymanager-3.4.0.GA.jar、hibernate3-3.3。 2.GA.jar、javassist-3.9.0.GA.jar、log4j-1.2.16.jar、mysql-connector-java-5.1.16.jar、openjpa-all-2.0.0-M3.jar、oscache- 2.4.1.jar, slf4j-api-1.5.2.jar, slf4j-log4j12-1.5.2.jar 当你使用 EJB3 你有ejb3-persistence.jar
文件吗?
我这里也没有使用 EJB。我没有 ejb3-persistence.jar 文件。我认为我们不需要这个文件,因为 entitymanager.jar 会处理这个。如果我错了,请纠正我。
检查this和this以上是关于休眠 JPA 错误的主要内容,如果未能解决你的问题,请参考以下文章