Why do I have to persist both objects if Address is nested in User?

后端 未结 3 1342
无人共我
无人共我 2021-02-09 05:54

I am trying to better familiarize myself with JPA so I created a very simple project. I have a User Class and an Address class. It appears that I have to persist both even tho

3条回答
  •  没有蜡笔的小新
    2021-02-09 06:25

    Try the cascade element for the annotation.

    @OneToMany(fetch=FetchType.LAZY, mappedBy="user", cascade=CascadeType.PERSIST) 
    private List
    addresses;

    The documentation says that by default no operation is cascaded. It also states that the cascade operation is optional, so it really depends on the implementation that you are using.

    Also, while setting the relationship, make sure you set both sides of the relationship. Set addresses to the user and user to the addresses.

提交回复
热议问题