2016-12-16 3 views
1

Ich habe 4 Tabellen.Löschen Einheit mit Composite Key von Primärschlüssel

Land (id, name), CountryType (id, name), Kunde (id, name) und Country_CountryType_Client Beziehung Tabelle (country_id, countryType_id, client_id).

Hier ist meine 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..*/ 

Die CountryType und Client classes gleich aussehen.

Hier ist meine CountryCountryTypeClient Klasse:

@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 ... */ 

Hier ist meine CountryCountryTypeClientPK Klasse:

@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 ..*/ 

Meine CountryCountryTypeClientRepository Klasse:

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

für mein Land Klasse habe ich CountryService Klasse mit saveCountry-Methode:

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; 
    } 

Die Frage ist: Wie kann ich alle Zeilen aus meiner Country_CountryType_Client Tabelle durch country_id löschen. Nicht nach PK, sondern nach Country_ID. Wenn ich das Land in meiner Landtabelle speichere, wird der Country_CountryType_Client automatisch mit den entsprechenden Werten aktualisiert.

Kleines Beispiel, nur um das aktuelle Problem zu beseitigen. In meiner Country_CountryType_Client-Tabelle habe ich jetzt dies.

here enter

Und nun möchte ich die newCountry speichern, die alle die gleiche Beziehung mit Ausnahme der letzten Zeile hat (298-2-9). Mein NewCountry weiß nichts über (298-2-9 Beziehung). Vor dem Speichern muss ich alle Zeilen löschen, die 298 ID haben.

Ich hoffe, das Problem ist klar.

Antwort

0

Ich verstehe das Problem nicht wirklich. Scheint, wie alle wirklich wollen Sie ist 298.

daher eine einzige CountryCountryTypeClient aus dem Land mit der Kennung entfernen, wenn Sie in Ihrer Zuordnung zu aktualisieren sind wie im folgenden beschrieben:

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; 
    } 

Sie können dann einfach wie folgt vorgehen:

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