2010-11-07 41 views
0

我正在学习Hibernate和Play框架(同时也将Java添加到帐户中...)。我在保存这种实体休眠中的关系问题

@Entity 
@Table(name="users") 
public class User extends Model { 



     @Required 
    public String username; 

     @Column(name="user_displayname",nullable=true) 
    public String displayname; 

     @Password 
     public String user_password; 

     @Email 
     @Column(name="user_email",nullable=false,unique=true) 
     public String user_email; 

     public String user_salt; 

     public Date user_joindate; 

     @ManyToOne 
     @JoinTable(name="users_meta") 
     public UserMeta userdata; 

     @Required 
     public boolean user_isActive; 

     @OneToOne(targetEntity=UserPhotos.class,cascade=CascadeType.ALL) 
     @JoinColumn(name="id",referencedColumnName="userID") 
     public UserPhotos userPhoto; 

     @ManyToMany(cascade=CascadeType.ALL) 
     @JoinTable(name="links_rol2user") 
     public List<Rol> rol; 
     public User (String username, models.Pass password, String user_email) { 
      this.username = username; 
      this.user_password = password.getHashedPassword(); 
      this.user_salt = password.getUserHash(); 
      this.user_email = user_email; 
      this.user_joindate = new Date(); 
      this.user_isActive = false; 

     } 

。这是当我注册用户

 // check if the validation has errors 
     if(validation.hasErrors()) { 
      params.flash(); // add http parameters to the flash scope 
      validation.keep(); // keep the errors for the next request 
      register(); 
     } else { 
       Cache.delete(uuid); 
       Pass pass = new Pass(password,new Date().toString()); 
       User newUser = new User(firstName, pass, email); 
       UserMeta utest = new UserMeta(newUser.id); 
       utest.setUserTownID(pueblos); 
       newUser.setUserMeta(utest); 
       newUser.save(); 
       Logger.info("NewUser ID : %s", newUser.getId()); 
//    UserMeta userInfo = new UserMeta(newUser.getId()); 
//    userInfo.setUserTownID(pueblos); 
//    userInfo.save(); 

       // TODO salvar foto a null 

       // Confirmation left 
       Cache.set("thankyou", "alright!", "3mn"); 
       thankyou(); 
     } 

我试图挽救userMeta我的代码的问题,它会创建一个新的记录当我将userMeta对象设置为newUser(现在不可见)时,但它不插入在newUser中创建的新ID。

我需要什么样的关系?在我现在调整代码之前,这是一个OneToOne关系,工作得很好,但现在当我完成注册功能时,它有点打我,我也需要保存userMeta对象..

如果您需要更多信息让我知道,我不知道如果我解释它好不好,只是想获得的休眠怎么办关系的窍门等

添加UserMeta:

*/

@Entity 
@Table(name="users_meta") 
public class UserMeta extends Model { 

    @Lob 
    @Column(name="userBio") 
    public String userBio; 
    @Column(name="userPhotoID",nullable=true) 
    public Long userPhotoID = null; 
    @Column(name="userRoleID", nullable=false) 
    public Long userRoleID = 2L; 


    @Lob 
    public String userDescription; 
    @Column(name="userViews", nullable=false) 
    public Long userViews = 0L; 
    @Column(name="userFavoriteCount", nullable=false) 
    public Long userFavoriteCount = 0L; 
    @Column(name="userTotalComments", nullable=false) 
    public Long userTotalComments = 0L; 
    @Column(name="userTotalUploadedVideos", nullable=false) 
    public Long userTotalUploadedVideos = 0L; 

    public Long userTownID; 


    public Long userID; 
    public UserMeta() {} 
    public UserMeta(Long userid) { 
     this.userBio = "El usuario no ha escrito nada todavia!"; 
     this.userDescription = "El usuario todavia no se ha describido!"; 
     this.userID = userid; 

    } 
    public Long getUserTownID() { 
     return userTownID; 
    } 

    public void setUserTownID(Long userTownID) { 
     this.userTownID = userTownID; 
    } 
} 

//合格模型

public class Pass { 

    protected String hashed; 
    protected String userHash; 

    public Pass(String passwordToHash, String salt) { 

     StringBuffer passSalt = new StringBuffer(passwordToHash); 
     this.userHash = DigestUtils.md5Hex(salt); 
     passSalt.append(this.userHash); 
     passSalt.append(Play.configuration.getProperty("application.passwordSalt")); 

     this.hashed = DigestUtils.sha512Hex(passSalt.toString()); 

    } 
    public String getHashedPassword() { 
     return this.hashed; 
    } 
    public String getUserHash() { 

     return this.userHash; 
    } 

}

+0

UserMeta和Pass类是怎么样的? – 2010-11-07 20:20:35

+0

现在编辑我的帖子,欢呼 – allenskd 2010-11-07 20:34:40

回答

1

似乎有很多事情在那里!但是从我所知道的问题来看,你的问题在于你传递给UserMeta的id。

当您在扩展Model时,该id将由Model类生成。但是,直到实体保存到数据库之后才会设置它(因为数据库自动生成该ID)。

因此,因为您在将用户对象保存之前将ID传递给UserMeta,所以id的值将为空。

如果您可以在创建UserMeta对象之前保存User对象,那么您的代码应该可以工作。

+0

我曾经这样做:),但然后getUserMeta(userData变量)持久性将继续。例如,我保存User对象,发生异常,并说明有关userData(带有连接的变量等),它必须是Transient或其他内容,因为只要它保存用户对象,它期望变量也有一些数据存储。 :(......好吧,因为我得到了我的酷回来(有几次尝试后有点疯了)我会尝试找出该怎么做,任何建议是欢迎与武器全开:) – allenskd 2010-11-07 22:04:52

+0

一个选项将扩展GenericModel而不是模型,并创建您自己的ID,或者通过user_email进行链接,因为它是唯一的,您的价值在前面。我希望知道当你首先保存你的用户对象时你的错误是什么。我不确定为什么会失败。 – Codemwnci 2010-11-07 22:24:59

+0

我很抱歉,因为迟到的答案,我忙于工作。谢谢您的帮助! – allenskd 2010-11-11 20:24:58