【问题标题】:I am getting this error "Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister"?我收到此错误“无法获取 org.hibernate.persister.entity.SingleTableEntityPersister 的构造函数”?
【发布时间】:2020-08-23 21:08:09
【问题描述】:

我正在尝试制作一个 spring-hibernate 应用程序,但出现此错误

Request processing failed; nested exception is org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister

我知道当它试图查找 getter 和 setter 但无法获取它们时会出现此错误。但我正确检查了我的 getter 和 setter,它们是正确的。

请帮帮我。

我的结构是这样的

代码是

  1. Employee.java
package com.wipro.config;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="empdata")
public class Employee {
    
    @Id
    private String empname;
    @Column(name="designation",length=15)
    private String designation;
    @Column(name="email",length=25)
    private String email;
    
    public String getEmpname() {
        return empname;
    }
    public void setEmpname(String empname) {
        this.empname = empname;
    }
    public String getDesignation() {
        return designation;
    }
    public void setDesignation(String designation) {
        this.designation = designation;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    
    
    
}
  1. EmployeeDao.java
package com.wipro.config;

import java.io.File;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class EmployeeDao {

    private SessionFactory factory;
    private Session session;
    private Transaction t;
    
    public EmployeeDao() {
        
    }
    
    public void saveData(Employee e) {
        
        factory =new Configuration().configure(new File("F:/Wipro teachings/springg/crud/src/main/java/com/wipro/config/hibernate.cfg.xml")).buildSessionFactory();
            session=factory.openSession();
            t=session.beginTransaction();
            session.save(e);
            t.commit();
        
    }

}
  1. ControllerDemo.java
package com.wipro.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wipro.config.Employee;
import com.wipro.config.EmployeeDao;

@Controller
public class ControllerDemo {
    private ApplicationContext conn;
    
    public ControllerDemo() {
        
    }
    @RequestMapping("/home")
    public String view1() {
        return "home";
    }
    
    @RequestMapping("/register")
    public String view2(Model m) {
        conn=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        Employee emp=conn.getBean("info",Employee.class);
        m.addAttribute("bean",emp);
        return "register";
    }
    
    @RequestMapping("/store")
    public String view3(@ModelAttribute("bean") Employee e,Model m) {
        conn=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        EmployeeDao obj=conn.getBean("dao",EmployeeDao.class);
        obj.saveData(e);
        m.addAttribute("msg","Record Inserted Successfully");
        return "register";
    }
    
}
  1. hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.password">Musu</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@Localhost:1521:orcl</property>
        <property name="hibernate.connection.username">sys as sysdba</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle8iDialect</property>
        <property name="dialect">org.hibernate.dialect.Oracle8iDialect</property> 
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>  
    
    <mapping class="com.wipro.config.Employee"/>
    
    
    </session-factory>
</hibernate-configuration>
  1. ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>

<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
               
   
<bean id="info" class="com.wipro.config.Employee"></bean>
 
 <bean id="dao" class="com.wipro.config.EmployeeDao"></bean>              
</beans>             
  1. home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="register">Register</a>
</body>
</html>
  1. register.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
    <%@page isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>Register Page</h1>
    <form:form action="store" method="post" modelAttribute="bean">
        Enter name=<form:input path="empname"/><br><br>
        Enter designation=<form:input path="designation"/><br><br>
        Enter email=<form:input path="email"/><br><br>  
    <input type="submit" value="register">
    </form:form>
    <br>
    <h2>${msg}</h2>
    
</body>
</html>
  1. spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
        
        <context:component-scan base-package="com.wipro"></context:component-scan>
        <mvc:annotation-driven></mvc:annotation-driven>
        <bean id="vr" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        
        </beans>

  1. web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
  1. index.jsp
<html>
<body>
<h2>Hello World!</h2>
<a href="home">Home</a>
</body>
</html>

【问题讨论】:

  • 请不要向您的实体 Employee 添加任何 args 默认构造函数,看看会发生什么。 public Employee(){}
  • 试过还是一样的错误。
  • 这个错误非常广泛。 Internet 显示在从 Java 9 迁移到 Java 11 或更新休眠版本或 pom.xml 中缺少 javassist 时出现此错误的用户。你试过什么了? Java 版本是什么,您是否尝试过切换休眠版本或将 javassist 添加到 pom.xml 中?
  • 您可能正在混合来自不同版本的 Hibernate 的 jar,这是您不应该做的。话虽如此,您的代码也有缺陷,1.您应该使用 spring 来管理 Hibernate Session Factory 2.不要创建应用程序上下文来获取 bean,最终您将耗尽内存和 3.该代码不是线程安全。
  • 你是对的。问题出在 pom.xml 文件中的版本上。我更改了文件,终于得到了输出。感谢@DanyloGurinov 和 M. Deinum 的帮助。

标签: java spring spring-boot hibernate spring-mvc


【解决方案1】:

我通过添加无参数构造器解决了这个问题。如果你使用的是 Lombok,只需要添加@NoArgsConstructor 注解即可:

@AllArgsConstructor
@NoArgsConstructor
@Getter
@ToString
@EqualsAndHashCode
public class User {
    private Long userId;
    private String shortName;
}

【讨论】:

    猜你喜欢
    • 2016-03-05
    • 2017-01-04
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    相关资源
    最近更新 更多