【问题标题】:insert value into one to one mapping annotation using ebeans or hibernate使用 ebeans 或休眠将值插入到一对一映射注释中
【发布时间】:2015-05-13 05:51:04
【问题描述】:

您好,我正在尝试使用具有用户和 user_profile 详细信息的 ebeans 实现 ebeans 一对一映射。我已经对每个键进行了注释标记,但无法在运行时插入值。

UserProfile.java

@Entity
public class UserProfile extends Model{

    @Id
    public Long userProfileId;

    public String empId;
    public String name;
    public String emailId;

    @OneToOne
    @JoinColumn(name = "email")
    public User user;

    //inserting filled form into database
    public static Long create(UserProfile userProfile)
    {
        //save leave into database
        userProfile.save();
        return userProfile.serial_id;
    }

用户.java

@Entity
public class User extends Model {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public static Finder<String, User> find = new Finder<String, User>(String.class, User.class);


    @Id
    public String email;
    public String name;
    public String password;
    public String permission;
    public int userProfileId;

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch= FetchType.LAZY)
    public UserProfile profile;

    public User(long id,String email, String name, String password,String permission,
                int user_id) {
        //this.id=id;
       this.email = email;
       this.name = name;
       this.password = password;
       this.permission = permission;
       this.user_id = user_id;
    }

我想将 userprofile 表的 userprofile_id 插入到用户的 userProfileId 中。

【问题讨论】:

    标签: java hibernate orm ebean


    【解决方案1】:

    尝试以下操作:

    @Entity
    public class UserProfile extends Model{
    
        @Id
        public Long userProfileId;
    
        public String empId;
        public String name;    
    
        @OneToOne
        public User user;
    
        //inserting filled form into database
        public static Long create(UserProfile userProfile)
        {
            //save leave into database
            userProfile.save();
            return userProfile.serial_id;
        }
    
    @Entity
    public class User extends Model {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        public static Finder<String, User> find = new Finder<String, User>(String.class, User.class);
    
    
        @Id
        public String email;
        public String name;
        public String password;
        public String permission;
    
        @OneToOne(cascade = CascadeType.ALL, fetch= FetchType.LAZY)
        public UserProfile profile;
    
        public User(long id,String email, String name, String password,String permission,
                    int user_id, UserProfile profileIn) {
            //this.id=id;
           this.email = email;
           this.name = name;
           this.password = password;
           this.permission = permission;
           this.user_id = user_id;
           this.profile = profileIn;
        }
    

    不同之处在于UserProfile 实体中不需要emailIdUser 实体中不需要userProfileId。只需使用实体实例,hibernate 就会完成它的工作。如果您完全需要在 User 实体中公开 userProfileId 以允许服务仅访问 id ,然后通过以下更改修改 User 类:

    @Column(name = "profileId", insertable = false, updatable = false, nullable = false)
    private Long profileId;
    
    @OneToOne(cascade = CascadeType.ALL, fetch= FetchType.LAZY, nullable = false)
    @JoinColumn(name = "profileId")
    private UserProfile profile;
    

    最后但并非最不重要的一点是,请不要像在课程中那样公开属性。将它们设为私有并实现 getter/setter 方法。

    【讨论】:

    • 我希望用户插入到用户配置文件表中时,用户配置文件 id 会自动插入到用户中。有可能吗??
    • 是的,有可能。持久化 UserProfile 实体后,只需在您的 User 实体实例上调用 entityManager.refresh() ,您将获得带有配置文件 ID 的更新用户实例。如果是这种情况,那么我宁愿从 User 实体构造函数中删除 UserProfile,将 User 添加为 UserProfile 类的构造函数参数,并在 User 类中为 UserProfile 添加一个 getter 方法。如果它对您有任何帮助,请不要忘记为我的回答投票。谢谢!
    • 插入时出现错误'ERROR execution DML bindLog[] error[Column 'profileId' cannot be null]'你能帮帮我吗?
    • 如果您在没有 UserProfile 的情况下保存用户,请从 profile 和 profileId 字段中删除可为空的约束。还要确保从数据库表中删除 NULL 约束。
    • 我无法插入值。你能给我代码吗。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    相关资源
    最近更新 更多