[英]Delete entity with Composite Key by Primary Key
I have 4 tables. 我有4张桌子。
Country (id, name), CountryType (id, name), Client (id, name) and Country_CountryType_Client relation Table (country_id, countryType_id, client_id). 国家 (id,名称), CountryType (id,名称), 客户 (id,名称)和Country_CountryType_Client关系表(country_id,countryType_id,client_id)。
Here is my Country class
: 这是我的
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..*/
The CountryType
and Client classes
look the same. CountryType
和Client classes
看起来相同。
Here is my CountryCountryTypeClient
class : 这是我的
CountryCountryTypeClient
类:
@GeneratePojoBuilder(
intoPackage = "*.builder")
@Entity
@Table(
name = "COUNTRY_COUNTRY_TYPE_CLIENT")
@AssociationOverrides({
@AssociationOverride(name= "pk.country",
joinColumns=@JoinColumn(name = "COUNTRY_ID")),
@AssociationOverride(name="pk.countryType",
joinColumns=@JoinColumn(name = "COUNTRY_TYPE_ID")),
@AssociationOverride(name="pk.client",
joinColumns=@JoinColumn(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 ... */
Here is my CountryCountryTypeClientPK
class : 这是我的
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 ..*/
My CountryCountryTypeClientRepository
class : 我的
CountryCountryTypeClientRepository
类:
public interface CountryCountryTypeRepository extends JpaRepository<CountryCountryTypeClient, CountryCountryTypeClientPK> {}
For my Country class I have CountryService class with saveCountry method : 对于我的Country类,我具有带有saveCountry方法的CountryService类:
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;
}
The question is: How can I delete all rows from my Country_CountryType_Client Table by Country_ID. 问题是:如何通过Country_ID从Country_CountryType_Client表中删除所有行。 Not by PK, but by Country_ID.
不是通过PK,而是通过Country_ID。 When I am saving the country in my Country Table, the Country_CountryType_Client is automatically updated with corresponding values.
将国家/地区保存在国家/地区表中时,Country_CountryType_Client将自动更新为相应的值。
Small example , just to clear the current problem. 小例子 ,只是为了清除当前问题。 In my Country_CountryType_Client Table now I have this.
现在在我的Country_CountryType_Client表中有这个。
And now I want to save the NewCountry that has all the same relation except the last row (298-2-9). 现在我想保存有除最后一行(298-2-9)都是一样的关系NewCountry。 My NewCountry dont know nothing about (298-2-9 relation).
我的NewCountry一无所知(298-2-9关系)。 Before saving I have to delete all rows that have 298 id.
保存之前,我必须删除所有具有298 id的行。
Hope the problem is clear. 希望问题解决。
I don't really understand the issue. 我不太了解这个问题。 Seems like all you really want to do is remove a single CountryCountryTypeClient from the Country with identifier 298.
似乎您真正想要做的就是从标识为298的国家/地区删除单个CountryCountryTypeClient。
Therefore if you were to update your mapping in as outlined in the following: 因此,如果要按照以下概述更新映射:
https://docs.oracle.com/cd/E19798-01/821-1841/giqxy/ 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;
}
You can then simply do as follows: 然后,您可以简单地执行以下操作:
Country country = // the country with id 298
CountryCountryTypeClient client = // the client with id 298/2/9
country.getCountryCountryTypeClient().remove(client);
countryRepository.save(country);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.