有什么方法可以使用JPA从子实体中删除实体?

时间:2018-09-26 10:29:23

标签: spring-boot jpa

我有3个表,分别为表A,B,C。表A与表B关联@OneToMany。表B与表C关联@ManyToOne。现在,当我按表A的ID查找时,便可以获取详细信息A,B,C。但是当我坚持/删除时,它仅影响表A&B。表C不受影响。

在JPA中是否可以从子实体中删除实体?搜索了很多,但找不到任何线索。

下面是所有三个表的实体模型

@Entity
    @Table(name = "FEATUREMASTER")
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

    public class FeatureMaster implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Id 
        @Column(name = "FGID")
        private String featureid;

        @Column(name = "FEATURENAME", nullable = false, unique = false)
        private String featurename;

        @Column(name = "DESCRIPTION", nullable = true, unique = false)
        private String description;

        @Column(name = "LIBNAME", nullable = true, unique = false)
        private String libname;

        @Column(name = "ISENABLED", nullable = false, unique = false)
        private String isenabled;

        @Column(name = "EDRULEGRP", nullable = true, unique = false)
        private String edrulegrp;

        // Do Not use - [orphanRemoval = true & CascadeType.ALL]- If used, deletion is not happening 
        @OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
        @JoinColumn(name = "FGID")
        private List<CfgMaster> parameters;

// Getters and Setters
}


    @Entity
    @Table(name = "CFGMASTER")
    public class CfgMaster implements Serializable {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @EmbeddedId
        private CfgMasterPK id;

        @Column(name = "CONFIGNAME", length = 45, nullable = true, unique = false)
        private String parameter_name;

        @Column(name = "CONFIGTYPE", length = 20, nullable = true, unique = false)
        private String type;

        @Column(name = "SUBPARAM", nullable = true, unique = false)
        private Integer subparam;

        @Column(name = "CONFIGDESCRIPTION", nullable = true, unique = false)
        private String description;

        @Column(name = "CONFIGLIMITFROM", nullable = true, unique = false)
        private String from;

        @Column(name = "CONFIGLIMITTO", nullable = true, unique = false)
        private String to;

        @ManyToOne(cascade = {CascadeType.ALL}, optional = true, fetch = FetchType.LAZY )
    //  @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
        @NotFound(action=NotFoundAction.IGNORE) // This is required to handle when no CfgData is found
        @JoinColumns({
                @JoinColumn(name = "FGID", insertable = false, updatable = false),
                @JoinColumn(name = "DATAKEY", insertable = false, updatable = false) 
                })
        private CfgData cfgData;

//Getters and Setters
    }

@Entity
@Table(name = "CFGDATA")
public class CfgData implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    /*@EmbeddedId
    private CfgDataPK id;*/

    @Id
    @Column(name = "FGID")
    private String fgid;

    @Id
    @Column(name = "DATAKEY")
    private String datakey;

    @Column(name = "EPID", nullable = false, unique = false)
    private int epid;


    @Column(name = "RESERVED1", length = 45, nullable = true, unique = false)
    private String reserved1;

    @Column(name = "VALUE1", length = 100, nullable = true, unique = false)
    private String value1;

    @Column(name = "VALUE2", length = 100, nullable = true, unique = false)
    private String value2;
     //Getters and Setters
}

我面临的问题是,我无法通过传递FeatureMaster的主要ID来删除/保存CfgData的实体。我所做的任何操作都只会影响父级&子级,而不会影响我尝试了很多谷歌搜索的大子级(CfgData),但是我找不到解决方案。

0 个答案:

没有答案