Java: Hibernate @OneToOne mapping

后端 未结 2 649
猫巷女王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.

    0 讨论(0)
  • 2020-12-01 05:06

    Your Status entity must not have properties userId and contentId of type Integer, mapped with @Column. It must have properties user and content of type User and Content, mapped with @OneToOne:

    public class User {
        @OneToOne(mappedBy = "user")
        private Status status;
        // ...
    }
    
    public class Status {
        @OneToOne
        @JoinColumn(name = "frn_user_id")
        private User user;
        // ...
    }
    

    A user has one status. A status has one user.

    0 讨论(0)
提交回复
热议问题