0

I'll put it simple. I've got User class and Privilege class. User has many Privileges.

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user") private Set<Privilege> privileges; 

Privilege has one and only one User.

@ManyToOne @JoinColumn(name = "user_id", nullable = false) private User user; 

As you see I've specified CascadeType to ALL, but whenever I want to persist my User:

 Set<Privilege> privs = new HashSet<>(); Privilege priv = new Privilege("anything"); //priv.setUser(user); it works with this line, of course privs.add(priv); user.setPrivileges(privs); //session.save(user); 

Privilege has not binded user. Any ideas?

@Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id") private Long id; @Column(name="email", nullable = false, unique = true) private String email; @Column(name="password") private String password; @Column(name="user_type") @Enumerated(EnumType.STRING) private UserType userType; @OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY, mappedBy = "user") private Set<Privilege> privileges = new HashSet<>(); 

//getters, setters

@Entity @Table(name = "privileges", uniqueConstraints = @UniqueConstraint(columnNames = {"user_id", "privilege"})) public class Privilege { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id; @Column(name = "privilege") private String privilege; @ManyToOne @JoinColumn(name = "user_id", nullable = false) private User user; 

//getters setters

4
  • Is your Privilege class also an entity? Commented Jan 19, 2016 at 14:23
  • Yes and Privileges are saved whenever I save User. Thing is, without User binded. Commented Jan 19, 2016 at 14:25
  • have you set the other side of the relation? Commented Jan 19, 2016 at 14:27
  • Please post both entities completely for better understanding and Also what you are expecting to save in database Commented Jan 19, 2016 at 14:28

2 Answers 2

2

To make sure I got your statement "Privilege has not binded user" . If you uncomment priv.setUser(user) then hibernate is able to associate user with the privilege in the database (i.e., user_id field is getting populated properly in the Privilege table). And if you comment out this line you don't see user_id being associated in the privilege table. Is that right?

If so, the reason is, you have specified mappedBy=user in the oneToMany annotation. This informs the hibernate that the association is mananged by the User field in the Privilege. So when hibernate is inserting the privilege record it looks into the user field to populate the userID.

With priv.setUser(user) hibernate would now know to which user this privilege has to be associated with and if you don't set this it will be null and you would see a null value against user_id column.

Or, let me know if I misinterpreted the question.

Sign up to request clarification or add additional context in comments.

Comments

0

When you the Hibernate save process will causing a ACTION_SAVE_UPDATE action, but the JPA will pass a ACTION_PERSIST and ACTION_MERGE, it will not match and causing the cascade failed to execute.

If you delete the JPA cascade – javax.persistence.CascadeType, replace it with Hibernate cascade – org.hibernate.annotations.Cascade, with CascadeType.SAVE_UPDATE.

It must work.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.