0
任何人都可以幫我解決這個問題。 我的配置是使用Maven: - 休眠4.3.6 - servlet的3.0.1 - MySQL的5.1.12 - 作爲JBoss 7.1.1無法使用Hibernate和Servlet創建SessionFactory
當我嘗試運行我的申請,我得到這個錯誤信息:
11:15:07,298 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) Initial SessionFactory creation failed.java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
11:15:07,301 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/Guru].[com.guru.controller.EmployeesServlet]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet com.guru.controller.EmployeesServlet threw exception: java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
at org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:2881) [hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1795) [hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963) [hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796) [hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788) [hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742) [hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410) [hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) [hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928) [hibernate-core-4.3.6.Final.jar:4.3.6.Final]
at util.HibernateUtil.<clinit>(HibernateUtil.java:13) [classes:]
at com.guru.dao.EmployeesDaoImpl.getEmployee(EmployeesDaoImpl.java:36) [classes:]
at com.guru.business.EmployeesBusinessImpl.getEmployee(EmployeesBusinessImpl.java:19) [classes:]
at com.guru.controller.EmployeesServlet.doGet(EmployeesServlet.java:29) [classes:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_60]
我的代碼:
@WebServlet("/employee")
public class EmployeesServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Employees> employees = new ArrayList<Employees>();
IEmployeesBusiness employeesBusiness = new EmployeesBusinessImpl();
Employees employee = employeesBusiness.getEmployee(1056);
request.getSession().setAttribute("employee", employee);
response.sendRedirect("employees.jsp");
}
}
employes.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Employees</title>
</head>
<body>
<h1>Employees</h1>
<hr>
<c:out value="${employee}"></c:out>
</body>
</html>
MAVEN
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.guru</groupId>
<artifactId>Guru</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Guru Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.12</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
</dependencies>
<build>
<finalName>Guru</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
此類測試工作正常,但是當我嘗試在JSP中顯示的結果,我得到的錯誤 告訴我,會話不能初始化
public class Test {
public static void main(String[] args) {
Employees employee = new Employees();
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
List<Employees> employees = new ArrayList<Employees>();
IEmployeesBusiness employeesBusiness = new EmployeesBusinessImpl();
Employees emp = employeesBusiness.getEmployee(1056);
session.getTransaction().commit();
session.close();
System.out.println(emp.getFirstName());
}
}
發佈您的代碼將非常有幫助 – 2014-08-30 09:21:18
@cozia在我的映射中存在一些問題。檢查如何配置實體之間的關聯 – Skizzo 2014-08-30 14:10:14