我正在学习 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 对象..
如果您需要更多信息,请告诉我,我不知道我是否解释得很好,只是想了解 Hibernate 如何处理关系等。
添加用户元:
*/
@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;
}
}