【问题标题】:I am getting hibernate exception when i am saving the data as No default constructor for entity ? Where i am doing wrong当我将数据保存为 No default constructor for entity 时出现休眠异常?我在哪里做错了
【发布时间】:2017-05-05 18:46:57
【问题描述】:

数据库表:

  1. 创建表用户 ( uid 序列号 NOT NULL, user_id 字符变化(10)NOT NULL, 密码字符变化(50), screen_name 字符变化(100)NOT NULL, auth_type 整数 NOT NULL, active_flag boolean NOT NULL, delete_flag boolean NOT NULL, created_by 整数 NOT NULL, created_on 时间戳,时区不为空, modified_by 整数 NOT NULL, 带有时区 NOT NULL 的 modified_on 时间戳, 约束 user_pkey 主键 (uid), 约束 user_user_id_key 唯一(user_id) )

  2. 创建表首选项( UID 序列号不为空, USER_ID 整数 NOT NULL, 主题人物不同(50), 模板字符变化(50), CREATED_BY 整数 NOT NULL, 时区不为空的 CREATED_ON 时间戳, MODIFIED_BY 整数 NOT NULL, MODIFIED_ON 时间戳,时区不为空, CONSTRAINT PREFERENCES_PKEY PRIMARY KEY (USER_ID), CONSTRAINT PREFERENCES_FKEY FOREIGN KEY (USER_ID) REFERENCES USER (UID) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION

);

我们的应用程序正在使用 JPA 注释

使用注解的休眠映射

由于安全原因,我无法上传整个文件

user.java 文件是这样的

我已经定义了属性以及首选项的属性

私人用户偏好用户偏好;

我正在做映射

line 1: @OneToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
line2: @JoinColumn(name="UID",referencedColumnName="USER_ID")

preferences.java 文件

这是我的用户偏好文件

@Entity
@Table(name="USER_PREFERENCES")
public class UserPreferences {
    private int uid;
     private String userId;
     private String theme;
     private String template;

    public UserPreferences(int uid, String userId, String theme, String template, RecordDetails recordDetails) {
        super();
        this.uid = uid;
        this.userId = userId;
        this.theme = theme;
        this.template = template;

    }


    @Id
    @Column(name="UID",updatable=false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }


    @Column(name="USER_ID", updatable=false, unique=true)
    public String getUserId() {
        return userId;
    }


    public void setUserId(String userId) {
        this.userId = userId;
    }


    @Column(name="THEME")
    public String getTheme() {
        return theme;
    }


    public void setTheme(String theme) {
        this.theme = theme;
    }

    @Column(name="TEMPLATE")
    public String getTemplate() {
        return template;
    }

    public void setTemplate(String template) {
        this.template = template;
    }

}

根据我在其中给出的数据库表的所有属性。

我有用户列表,我想为每个用户分配一个偏好,所以我进行了一对一的映射

当首选项表没有行时,我可以查看用户列表并为任何用户添加首选项。回来并获得用户列表后,我看到一个错误,因为

实体没有默认构造函数:UserPreferences

你能帮我解释一下为什么我只有在优先添加一行之后才会得到这个我该如何解决它。

在下面一行我遇到了一个异常

Criteria 标准 = session.createCriteria(User.class); usersList = criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

提前致谢

【问题讨论】:

  • 嗨,蒂姆,我已经浏览了链接,两者都不一样。
  • 如果您缺少默认构造函数,则重复链接是合适的。
  • 请向我们展示您的班级用户偏好。
  • 克里斯我更新了问题中的首选项表?请帮助我在哪里做错了

标签: java hibernate jpa


【解决方案1】:

由于定义了参数构造函数,因此需要显式定义默认构造函数。

只需添加

public UserPreferences() {
    // needed for JPA
}

到你的班级。

Hibernate 使用反射来实例化实体类。反射使用Class<T>.newInstance() 来做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 2021-12-15
    • 2015-03-21
    • 2022-01-17
    相关资源
    最近更新 更多