第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

當(dāng)孩子更新時(shí)如何更新jpa中雙向關(guān)系所有者的fk

當(dāng)孩子更新時(shí)如何更新jpa中雙向關(guān)系所有者的fk

qq_笑_17 2023-07-19 16:02:30
第一個(gè)定義簡單地說,我有兩個(gè)實(shí)體,第一個(gè)是用戶,第二個(gè)是地址,每個(gè)用戶可以有一個(gè)或多個(gè)地址。這意味著,我想要用戶和地址之間的雙向 OneToMany 關(guān)系。問題我有一個(gè)具有 3 個(gè)地址的用戶(具有 3 個(gè)地址列表的用戶),當(dāng)我更新用戶并刪除一些地址(具有 2 個(gè)地址列表的用戶)時(shí),保存用戶后,地址的 fk 不會(huì)更新。我預(yù)計(jì)更新后我的新用戶有 2 個(gè)地址,但地址并未更改。簡而言之:當(dāng)我從用戶更新地址表的列表時(shí),地址表的 fk 沒有更新。用戶實(shí)體(只寫地址字段)@Entity@Table(name = TABLE_NAME)public class User {public static final String TABLE_NAME = "User";//Some Fields Like ID  , name ,...@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")@JsonManagedReferenceprivate List<Address> addresses;public List<Address> getAddresses() {    return addresses;}public void setAddresses(List<Address> addresses) {    this.addresses = addresses;}}地址實(shí)體(僅寫入用戶字段)@Entity@Table(name = TABLE_NAME)public class Address {public static final String TABLE_NAME = "ADDRESS";//Some Fields Like ID  , name ,...@ManyToOne(cascade = CascadeType.ALL)@JoinColumn(name = "user_id")@JsonBackReferenceprivate User user;public User getUser() {    return user;}public void setUser(User user) {    this.user = user;}}服務(wù)等級@Servicepublic class UserService@AutowiredUserRepository repository;public updateUserAddress() {    //Get User With Id of 1    User user = repository.findById(1L);    //Get Addresses    List<Address> addresses = user.getAddresses();    //Delete One Address From List    addresses.remove(0);    //Set New Address to User Entity    user.setAddresses(addresses);    //Save User     repository.save(user)    //It doesn't work (List of Address of user is the same and not changed)}順便說一句,我使用 Spring-data-jpa 和 Hibernate 作為 DAO 層。
查看完整描述

1 回答

?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

通過在實(shí)體removeAddress中添加方法User,您可以更新表的fk address,也可以保留行:


public void removeAddress(Address address) {

    address.setUser(null); // or the value you want

}

該updateUserAddress方法應(yīng)該是這樣的:


public void updateUserAddress() {


    User user = repository.findById(1L);

    List<Address> addresses = user.getAddresses();


    Address address = addresses.get(0); //the address to be removed

    user.removeAddress(address); 


    user.setAddresses(addresses);


    repository.save(user);


}

用戶實(shí)體:


@Entity

@Table(name = "users")

public class User {

    // other fields


    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch=FetchType.EAGER)

    @JsonManagedReference

    private List<Address> addresses;


    // getters, setters, constructors etc.


    // the remove method

    public void removeAddress(Address address) {

        address.setUser(null);

    }

}

地址實(shí)體:


@Entity

@Table(name = "address")

public class Address {

    // other fields


    @ManyToOne

    @JoinColumn(name = "user_id")

    @JsonBackReference

    private User user;


    // getters, setters, constructors etc.

}


查看完整回答
反對 回復(fù) 2023-07-19
  • 1 回答
  • 0 關(guān)注
  • 151 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號