【问题标题】:Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.InstantiationException: No default constructor for entity: [duplicate]线程“主”javax.persistence.PersistenceException 中的异常:org.hibernate.InstantiationException:实体没有默认构造函数:[重复]
【发布时间】:2019-08-08 01:33:58
【问题描述】:

嗨,我是休眠新手。每当我试图从 MySQL 5.5 检索数据时,它都会抛出异常 Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.InstantiationException: No default constructor for entity:

但是当我尝试将数据保存在 sql 表中以使其正常工作时。 我也找到了这个problem 的解决方案,但它仍然显示相同的错误。

【问题讨论】:

  • 您收到哪个实体的错误?另外,请分享您尝试运行的代码。
  • 读取紧跟在您发布的部分消息之后的实体名称,然后读取消息,告诉您该实体缺少默认构造函数。所以,加一个。

标签: java hibernate


【解决方案1】:

确保您的实体中有无参数构造函数

@Entity
@Table(name = "employee")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "name")
    private String name;

    @Column(name="age")
    private Integer age;

    // A default constructor, one without agruments
    public Employee() {
    }


    // A constructor with agruments
    public Employee(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

   }

【讨论】:

  • 是添加了不带参数的默认构造函数,但它显示相同的错误
  • 您是否在所有实体中都检查过这个?
  • 嘿@geobudex 有一个拼写错误现在它工作正常谢谢你
猜你喜欢
  • 2017-10-20
  • 2018-01-10
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 2021-09-02
  • 2020-05-19
  • 1970-01-01
相关资源
最近更新 更多