无法使用Hibernate和Servlet创建SessionFactory

时间:2014-08-30 09:19:29

标签: java hibernate maven servlets

任何人都可以帮助我解决这个问题。 我的配置是使用Maven: - Hibernate 4.3.6 - servlet 3.0.1 - mysql 5.1.12 - Jboss AS 7.1.1

当我尝试运行我的应用程序时,收到此错误消息:

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中显示结果时,我得到了错误 告诉我Session无法初始化

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());

    }
}

1 个答案:

答案 0 :(得分:0)

尤里卡!我找到了。我将我的hibernate的版本更改为MAVEN依赖项中的4.3.6.Final。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>**4.3.6.Final**</version>
</dependency>

这个版本的hibernate与jpa 2.0兼容(但不与jpa 2.1兼容)。使用jpa 2.0,属性@JoinColumn(&#34; foreignKey =&#34;)不是必需的。

在我收到此错误之前: javax.persistence.JoinColumn.foreignKey()Ljavax /持久/ ForeignKey的;