Java: Hibernate @OneToOne mapping

后端 未结 2 668
猫巷女王i
猫巷女王i 2020-12-01 04:53

I\'m trying to get Hibernate @OneToOne annotations working and not having much success here...

Let\'s say I\'ve got a table called status that looks lik

2条回答
  •  盖世英雄少女心
    2020-12-01 05:03

    use this way. Add below field in Customer entity.

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "customer")
    private Status status;
    

    and don't add getter and setter for status field in Customer entity. And add below code to Status Entity.

    @OneToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "customer_id", nullable = false)
    private Customer customer;
    

    Here add the getter and setter for customer field in Status Entity class. You can see one working example here.

提交回复
热议问题