我有两个实体 User 和 Wish :
@Entity
@Table(name = "T_USER")
public class User implements Serializable {
@Column(length = 50)
private String lastname;
@Column(length = 50)
private String firstname;
@OneToMany(mappedBy = "user",cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Wish> wishes = new HashSet<Wish>();
// getters and setters
}
@Entity
@Table(name = "T_WISH")
public class Wish implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "title")
private String title;
@Column(name = "description")
private String description;
@ManyToOne
private User user;
// getters and setters
}
当我保存用户时,recruiter_id 为空,为什么。我已经尝试过了:
Wish wish = wishRepository.save(wish);
user.getWishes.add(wish);
User userSaved = userRepository.save(user);
为什么没有设置recruiter_id。
最佳答案
您的用户类没有@Id注释字段。
关于java - 为什么 Hibernate ManyToOne 没有正确持久化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28594980/