我正在学习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;
    }


}

最佳答案

那里似乎发生了很多事情!但是据我所知,您的问题是传递给UserMeta的ID。

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

因此,由于在保存User对象之前将id传递给UserMeta,所以id的值将为null。

如果可以在创建UserMeta对象之前保存User对象,则代码应该可以正常工作。

10-01 09:45