2016-12-16 93 views
1

我有4個表格。用主鍵刪除帶複合鍵的實體

國家(ID,姓名),CountryType(ID,姓名),客戶(ID,姓名),並Country_CountryType_Client關係表(COUNTRY_ID,countryType_id,CLIENT_ID)。

這裏是我的Country class

@GeneratePojoBuilder(
     intoPackage = "*.builder") 

@Entity 
@Table(name = "MD_COUNTRY") 
@SequenceGenerator(
     name = "SEQ_MD_COUNTRY", 
     sequenceName = "SEQ_MD_COUNTRY", 
     allocationSize = 1) 
public class Country implements Serializable { 

    private static final long serialVersionUID = -3313476149373055743L; 
    private Long md_country_id; 
    private String nameKey; 
    private List<CountryCountryTypeClient> cCTypeClients; 

    @Id 
    @GeneratedValue(
      generator = "SEQ_MD_COUNTRY") 
    @Column(
      name = "MD_COUNTRY_ID") 
    public Long getMd_country_id() { 
     return md_country_id; 
    } 

    public void setMd_country_id(Long md_country_id) { 
     this.md_country_id = md_country_id; 
    } 

    @Column(name = "MD_COUNTRY_NAME_KEY") 
    public String getNameKey() { 
     return this.nameKey; 
    } 

    public void setNameKey(String name) { 
     this.nameKey = name; 
    } 


    @OneToMany(fetch=FetchType.EAGER,mappedBy="pk.country",cascade=CascadeType.ALL) 
    public List<CountryCountryTypeClient> getCountryCountryTypeClient() { 
     return cCTypeClients; 
    } 

    public void setCountryCountryTypes(List<CountryCountryTypeClient> countryCountryTypeClient) { 
     this.cCTypeClient = countryCountryTypeClient; 
    } 
/* ... hashCode and equals methods..*/ 

CountryTypeClient classes看起來是一樣的。

這裏是我CountryCountryTypeClient類:

@GeneratePojoBuilder(
     intoPackage = "*.builder") 
@Entity 
@Table(
     name = "COUNTRY_COUNTRY_TYPE_CLIENT") 
@AssociationOverrides({ 
    @AssociationOverride(name= "pk.country", 
      [email protected](name = "COUNTRY_ID")), 
    @AssociationOverride(name="pk.countryType", 
      [email protected](name = "COUNTRY_TYPE_ID")), 
    @AssociationOverride(name="pk.client", 
      [email protected](name = "CLIENT_ID")) 
}) 
public class CountryCountryTypeClient implements Serializable{ 

    private static final long serialVersionUID = -879391903880384781L; 

    private CountryCountryTypeClientPK pk = new CountryCountryTypeClientPK(); 

    public CountryCountryTypeClient() {} 

    @EmbeddedId 
    public CountryCountryTypeClientPK getPk() { 
     return pk; 
    } 

    public void setPk(CountryCountryTypeClientPK pk) { 
     this.pk = pk; 
    } 

    @Transient 
    public Country getCountry(){ 
     return getPk().getCountry(); 
    } 

    public void setCountry(Country country) { 
     getPk().setCountry(country); 
    } 

    @Transient 
    public CountryType getCountryType(){ 
     return getPk().getCountryType(); 
    } 

    public void setCountryType(CountryType countryType) { 
     getPk().setCountryType(countryType); 
    } 

    @Transient 
    public Client getClient() { 
     return getPk().getClient(); 
    } 

    public void setClient(Client client) { 
     getPk().setClient(client); 
    } 

/* ... hashCode and equals ... */ 

這裏是我CountryCountryTypeClientPK類:

@Embeddable 
public class CountryCountryTypeClientPK implements Serializable { 

    private static final long serialVersionUID = -3934592006396010170L; 

    private Country country; 
    private CountryType countryType; 
    private Client client; 


    public CountryCountryTypeClientPK() {} 

    @ManyToOne 
    public Country getCountry() { 
     return country; 
    } 
    public void setCountry(Country country) { 
     this.country = country; 
    } 

    @ManyToOne 
    public CountryType getCountryType() { 
     return countryType; 
    } 
    public void setCountryType(CountryType countryType) { 
     this.countryType = countryType; 
    } 

    @ManyToOne 
    public Client getClient() { 
     return client; 
    } 
    public void setClient(Client client) { 
     this.client = client; 
    } 

/*... hashCode and equals methods ..*/ 

CountryCountryTypeClientRepository類:

public interface CountryCountryTypeRepository extends JpaRepository<CountryCountryTypeClient, CountryCountryTypeClientPK> {} 

爲我的國家I類有CountryService類saveCountry方法:

public Country saveCountry(final Country dtoCountry) { 
     //save NEW Country 
     if(dtoCountry.getId()==null){ 
      de.bonprix.global.masterdata.model.Country modelWithoutID = convertToModel(dtoCountry); 

      for (CountryCountryTypeClient cCTypeClient : modelWithoutID.getCountryCountryTypeClients()) { 
      cCTypeClient.setCountry(modelWithoutID); 
     } 
     return convertToDTO(this.countryRepository.saveAndFlush(modelWithoutID)); 
     } 

     //save EDITED Country  
     else if (!(dtoCountry.getId()==null)){ 
      de.bonprix.global.masterdata.model.Country modelWithID = convertToModel(dtoCountry); 

      for (CountryCountryTypeClient cCTypeClient : modelWithID.getCountryCountryTypeClients()) { 
       cCTypeClient.setCountry(modelWithID); 
       ccTypeClientRepository.delete(cCTypeClient); 
     } 
      return convertToDTO(this.countryRepository.saveAndFlush(modelWithID)); 
     } 
     return null; 
    } 

的問題是:我如何通過COUNTRY_ID刪除我Country_CountryType_Client表中的所有行。不是由PK,而是由Country_ID。 當我將國家保存在我的國家/地區表中時,Country_CountryType_Client將自動更新爲相應的值。

小例子,只是爲了清除當前的問題。 在我的Country_CountryType_Client表中,現在我有這個。

here enter

現在我想保存NewCountry有除最後一行(298-2-9)都是一樣的關係。我的NewCountry對(298-2-9關係)一無所知。保存之前,我必須刪除所有具有298個ID的行。

希望問題清楚。

回答

0

我真的不明白這個問題。好像你真正想做的事,如果你是在下面列出的更新您的映射從鄉村刪除單個CountryCountryTypeClient與標識符298

因此:

https://docs.oracle.com/cd/E19798-01/821-1841/giqxy/

@OneToMany(fetch=FetchType.EAGER,mappedBy="pk.country",cascade=CascadeType.ALL, orphanRemoval = true) 
    public List<CountryCountryTypeClient> getCountryCountryTypeClient() { 
     return cCTypeClients; 
    } 

然後你可以簡單地做如下操作:

Country country = // the country with id 298 
CountryCountryTypeClient client = // the client with id 298/2/9 
country.getCountryCountryTypeClient().remove(client); 
countryRepository.save(country);