使用ebeans或hibernate将值插入到一对一的映射注释中

时间:2015-05-13 05:51:05

标签: java hibernate orm ebean

您好我正在尝试使用带有user和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;
    }

User.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。

1 个答案:

答案 0 :(得分: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;
    }

区别在于emailId实体中UserProfileuserProfileId实体中User不需要userProfileId。只需使用实体实例,hibernate就可以完成它的工作。如果您完全需要在User实体中公开{​​{1}}以允许服务仅访问该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方法。