开发者

Relationships question in hibernate

I'm learning Hibernate and Play framework (also add Java into account...). I'm having problems saving this kind of entity

@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;

        }

This is my code when I'm registering a user

        // 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();
        }

I'm trying to save the userMeta, it does creates a new record when I set the userMeta object into newUser (not visible right now), but it doesn't insert the new ID created in newUser.

What kind of relation do I need? before I tweaked the code as it is now, it was a OneToOne re开发者_JS百科lationship, worked quite well, but now when I was completing the register functions it kinda hit me that I needed to save userMeta object too..

If you need more info let me know, I don't know if I explained it well or not, just trying to get the hang of how Hibernate do relations, etc.

Adding 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;
    }
}

// pass model

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;
    }  

}


There seems to be a lot going on there! But from what I can tell, you problem is with the id that you are passing into the UserMeta.

As you are extending Model, the id is being generated by the Model class. However, this is not set until after the entity is saved to the database (as the id is auto-generated by the database).

Therefore, because you are passing the id into the UserMeta before the User object is saved, the value of id will be null.

If you can save the User object before you create your UserMeta object, your code should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜