我正在学习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如何做关系等等。
添加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 :(得分:1)
那里似乎有很多事情发生!但是从我所知道的,你的问题是你传递给UserMeta的id。
在扩展Model时,ID正由Model类生成。但是,直到将实体保存到数据库之后才会设置(因为id是由数据库自动生成的)。
因此,因为您在保存User对象之前将id传递给UserMeta,所以id的值将为null。
如果您可以在创建UserMeta对象之前保存User对象,则代码应该可以正常工作。