我正在学习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;
}
}
UserMeta和Pass类是怎么样的? – 2010-11-07 20:20:35
现在编辑我的帖子,欢呼 – allenskd 2010-11-07 20:34:40