当我将数据保存为实体的默认构造函数时,我得到了hibernate异常?我做错了什么

时间:2016-12-20 14:07:14

标签: java hibernate jpa

数据库表:

  1. CREATE TABLE用户 ( uid序列NOT NULL, user_id字符变化(10)NOT NULL, 密码字符变化(50), screen_name字符变化(100)NOT NULL, auth_type整数NOT NULL, active_flag布尔值NOT NULL, delete_flag boolean NOT NULL, created_by integer NOT NULL, created_on timestamp with time zone NOT NULL, modified_by integer NOT NULL, modified_on时间戳,时区为NOT NULL, CONSTRAINT user_pkey PRIMARY KEY(uid), CONSTRAINT user_user_id_key UNIQUE(user_id) )

  2. 创建表格首选项( UID序列NOT NULL, USER_ID整数NOT NULL, 主题角色各不相同(50), 模板字符各不相同(50), CREATED_BY整数NOT NULL, 带有时区的CREATED_ON时间戳NOT NULL, MODIFIED_BY整数NOT NULL, 带时区的MODIFIED_ON时间戳NOT NULL, CONSTRAINT PREFERENCES_PKEY PRIMARY KEY(USER_ID), CONSTRAINT PREFERENCES_FKEY FOREIGN KEY(USER_ID)REFERENCES USER(UID)简单匹配更新无操作删除无操作

  3. );

    我的申请我们正在使用JPA注释

    使用注释的hibernate映射

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

    user.java文件是这样的

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

    私人UserPreferences userPreferences;

    我正在做映射

    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

    请你帮我解释为什么我只有在优先添加一行后才能解决这个问题。如何解决它。

    在下面这行我得到了一个例外

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

    提前致谢

1 个答案:

答案 0 :(得分:0)

由于您定义了一个参数构造函数,因此需要明确定义默认构造函数。

添加

name

到你的班级。

Hibernate使用Reflection来实例化实体类。反射使用public UserPreferences() { // needed for JPA } 来执行此操作。