1

I have a one to one mapping relationship.

User.java

public class User {

    @Id
    @SequenceGenerator(name = "userID", sequenceName = "userID")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "userID")
    @Column(name = "id")
    private Long id;

    private String firstName;
    private String lastName;

    @Column(unique = true)
    private String username;
    private String password;

    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "id")
    private Role role;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", firstName=" + firstName + ", lastName="
                + lastName + ", username=" + username + ", password="
                + password + ", role=" + role + "]";
    }

}

Roles.java

public class Role {

    @Id
    @SequenceGenerator(name = "userID", sequenceName = "userID")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "userID")
    @Column(name = "roleId")
    private Long roleId;

    @OneToOne
    private User user;
    private Integer role;

    public Long getId() {
        return roleId;
    }

    public void setId(Long id) {
        this.roleId = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Integer getRole() {
        return role;
    }

    public void setRole(Integer role) {
        this.role = role;
    }
}

I am tring to save the data

public Long create(User user) {
    Long userID = super.save(user);
    return userID;
}

When I try to save the User entity I am seeing the data get saved as below.

User Table:

    id   firstName lastName   usrename   password

     1    hello     hello      hello     hello

Role table:

    roleId      role     user_id

     1          1         NULL

Can you tell me you the user table is not getting related each other.

I need user_id as 1 instead of NULL.

Here the id of user table is not getting mapped to user_id of table Role.

Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
bharathi
  • 6,019
  • 23
  • 90
  • 152
  • Put the mappedBy on the Role entity and remove it from User, if it is the User that owns the Role. – maress Jun 27 '14 at 13:40

1 Answers1

0
  • The Role is the owner of the association, the User's @OneToOne role doesn't need the @JoinColumn at all:

    @Id
    @SequenceGenerator(name = "userID", sequenceName = "userID")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "userID")
    @Column(name = "id")
    private Long id;
    
    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Role role;
    
  • When saving the user you need to also set both side of the association:

    user.setRole(role);
    role.setUser(user);
    userDao.create(user);
    
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • Can I know why I have to set both side association. I need user as the owner and the roles as the child. If @OnetoOne rolw does not need @JoinColumn? – bharathi Jun 30 '14 at 06:23
  • Every bidirectional association [must set both sides](http://stackoverflow.com/questions/5460573/hibernate-bidirectional-manytoone-updating-the-not-owning-side-not-working). – Vlad Mihalcea Jun 30 '14 at 06:26
  • I need a unidirectional way of adding. If I add user it should save roles of the particular user as well. whether it is possible to have this. Sorry for asking these types of questions. I know I am asking silly questions. Can you please help me on this to have unidirectional cascade association. – bharathi Jun 30 '14 at 07:28