为什么Hibernate ManyToOne没有正确持久化?

时间:2015-02-18 22:14:19

标签: java hibernate many-to-one

我有两个实体User and 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为null为什么。我试过了:

Wish wish = wishRepository.save(wish);
        user.getWishes.add(wish);
    User userSaved =    userRepository.save(user);

为什么没有设置recruiter_id。

1 个答案:

答案 0 :(得分:0)

您的用户类没有@Id anotatated字段。