Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按主键删除具有复合键的实体_Java_Hibernate_Jpa_Many To Many_Spring Data - Fatal编程技术网

Java 按主键删除具有复合键的实体

Java 按主键删除具有复合键的实体,java,hibernate,jpa,many-to-many,spring-data,Java,Hibernate,Jpa,Many To Many,Spring Data,我有四张桌子 国家(id,名称)、国家类型(id,名称)、客户(id,名称)和国家/地区-国家/地区-国家/地区-客户关系表(国家/地区id,国家/地区-客户id) 这是我的国家/地区课程: @GeneratePojoBuilder( intoPackage = "*.builder") @Entity @Table(name = "MD_COUNTRY") @SequenceGenerator( name = "SEQ_MD_COUNTRY",

我有四张桌子

国家(id,名称)、国家类型(id,名称)、客户(id,名称)和国家/地区-国家/地区-国家/地区-客户关系表(国家/地区id,国家/地区-客户id)

这是我的
国家/地区课程

@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..*/
这是我的
countrytypeclientpk
类:

@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 ... */
@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
CountryTypeClientRepository
类别:

public interface CountryCountryTypeRepository extends JpaRepository<CountryCountryTypeClient, CountryCountryTypeClientPK> {}
问题是:如何按国家ID从我的Country\u CountryType\u客户表中删除所有行。不是按主键,而是按国家ID。 当我在我的国家/地区表中保存国家/地区时,country\u CountryType\u客户端会自动更新相应的值

小示例,只是为了解决当前的问题。 在我的Country\u CountryType\u客户表中,现在我有了这个

现在我想保存一个新的国家,除了最后一行(298-2-9)之外,它的关系都是一样的。我的新国家对(298-2-9关系)一无所知。在保存之前,我必须删除所有具有298 id的行


希望问题清楚

我不太明白这个问题。似乎您真正想要做的就是从标识符为298的国家/地区删除单个CountryTypeClient

因此,如果要按照以下所述更新中的映射:


我真的不明白这个问题。似乎您真正想要做的就是从标识符为298的国家/地区删除单个CountryTypeClient

因此,如果要按照以下所述更新中的映射:

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