简体   繁体   English

将数据另存为实体的默认构造函数时,出现休眠异常? 我在哪里做错了

[英]I am getting hibernate exception when i am saving the data as No default constructor for entity ? Where i am doing wrong

Database tables: 数据库表:

  1. CREATE TABLE user ( uid serial NOT NULL, user_id character varying(10) NOT NULL, password character varying(50), screen_name character varying(100) NOT NULL, auth_type integer NOT NULL, active_flag boolean 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 timestamp with time zone NOT NULL, CONSTRAINT user_pkey PRIMARY KEY (uid ), CONSTRAINT user_user_id_key UNIQUE (user_id ) ) CREATE TABLE user(uid serial NOT NULL,user_id字符变化(10)NOT NULL,密码字符变化(50),screen_name字符变化(100)NOT NULL,auth_type整数NOT NULL,active_flag布尔型NOT NULL,delete_flag布尔型NOT NULL,created_by整数NOT NULL,带有时区的NOT_NULL的created_on时间戳,notify _by整数NOT NULL,带有时区NOT NULL的Modifyed_on时间戳,CONSTRAINT user_pkey PRIMARY KEY(uid),CONSTRAINT user_user_id_key UNIQUE(user_id))

  2. CREATE TABLE PREFERENCES ( UID serial NOT NULL, USER_ID integer NOT NULL, Theme character varying(50), Template character varying(50), CREATED_BY integer NOT NULL, CREATED_ON timestamp with time zone NOT NULL, MODIFIED_BY integer NOT NULL, MODIFIED_ON timestamp with time zone NOT NULL, 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 创建表首选项(UID序列非空,USER_ID整数非空,主题字符变化(50),模板字符变化(50),CREATED_BY整数NOT NULL,带时区的CREATED_ON时间戳NOT NULL,MODIFIED_BY整数NOT NULL,MODIFIED_ON时间戳与时间区域NOT NULL,CONSTRAINT PREFERENCES_PKEY主键(USER_ID),CONSTRAINT PREFERENCES_FKEY外键(USER_ID)参考用户(UID)匹配简单更新不采取行动删除不采取行动

); );

I our application we are using JPA annotations 在我们的应用程序中,我们使用的是JPA批注

hibernate mapping using annotations 使用注释的休眠映射

Due to security reason i cant able to upload entire file 出于安全原因,我无法上传整个文件

user.java file is like this user.java文件是这样的

I have defined properties along with the property for preferences 我已经定义了属性以及首选项的属性

private UserPreferences userPreferences; 私人UserPreferences userPreferences;

I am doing mapping as 我正在做映射

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

preferences.java file preferences.java文件

This is my userpreference file 这是我的用户首选项文件

@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;
    }

}

all the properties as per the database table i have given in it. 根据我在其中提供的数据库表的所有属性。

I am having users list and i want to assign a preference for each user so i took one to one mapping 我有用户列表,我想为每个用户分配首选项,所以我采用了一对一映射

When there is no row for a preference table i can able to see the list of users and add the preference for any user. 当偏好表没有行时,我可以看到用户列表并为任何用户添加偏好。 once after coming back and getting the userlist i am seeing an error as 回来并获得用户列表后,我一次看到错误

No default constructor for entity: UserPreferences 没有实体的默认构造函数:UserPreferences

Can you please help me why i am getting this only after adding a row in preference how can i solve it. 您能帮我为什么为什么我只有在优先添加一行后才能得到这个,我该如何解决呢?

Here at the below line i am getting an exception 在下面的行中,我得到一个例外

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

Thanks in advance 提前致谢

Since you defined an argument-constructor, you need to explictly define the default constructor. 由于定义了参数构造函数,因此需要明确定义默认构造函数。

Just add 只需添加

public UserPreferences() {
    // needed for JPA
}

to your class. 上你的课。

Hibernate uses Reflection to instantiate the entity-classes. Hibernate使用Reflection实例化实体类。 Reflection uses Class<T>.newInstance() to do so. 反射使用Class<T>.newInstance()这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM