简体   繁体   English

JPA在删除时级联一个抽象/继承类

[英]JPA Cascade a abstract/inherit Class on Delete

I have an abstract class TreeNode . 我有一个抽象类TreeNode From him i inherit the classes Country and Location . 我从他那里继承了CountryLocation类。 Then I have a class Role that includes predefined roles like administrator, user etc. These two classes are mapped together in the MappedRole class. 然后,我有一个Role类,其中包括预定义角色,例如管理员,用户等。这两个类在MappedRole类中映射在一起。 The database table of mappedRole has columns treenode_id role_id und mappedRole_id. mappingRole的数据库表具有列treenode_id,role_id和mappingRole_id。 The tables in the database look alike I have a table for country and location because I create Treenode with Table_per_Class . 数据库中的表看起来很像,因为我使用Table_per_Class创建Treenode, Table_per_Class我有一个用于国家和位置的表。 Therefore the table MappedRole has no ForeignKey for country and location.The treenode_id column in mappedRole contains then the id of the class its mapped to. 因此,表MappedRole没有用于国家和地区的ForeignKey.mappedRole中的treenode_id列包含其映射到的类的ID。 JPA gets the relation with a big join on both tables. JPA在两个表上都加入了大量的连接。

This is my MappedRole Class: 这是我的MappedRole类别:

@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "TREENODE_ID", "ROLE_ID" }) }, name = "mappedrole")
@NamedQueries({ @NamedQuery(name = "mappedRole.checkIfUserExist", query = "SELECT count(mr.role) FROM MappedRole mr WHERE mr.role =?1") })
public class MappedRole implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @ManyToOne(cascade = { CascadeType.REMOVE }, fetch = FetchType.EAGER)
    @JoinColumn(name = "TREENODE_ID", nullable = false)
    private TreeNode treeNode;

    @ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH }, fetch = FetchType.EAGER)
    @JoinColumn(name = "ROLE_ID")
    private Role role;

TreeNode class: TreeNode类:

@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class TreeNode extends Observable {
        @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected long id;

and the location class: 和位置类:

@Entity
@Table(name = "standort")
@NamedQuery(name = "standort.findAll", query = "SELECT s FROM Standort s order by s.name desc")
public class Standort extends TreeNode implements Comparable<Standort>, Serializable {

If I now delete a location the mappedRole will not be deleted even though I have CascadeType.REMOVE . 如果我现在删除一个位置,即使我具有CascadeType.REMOVE ,也不会将被映射的角色删除。 How can i make jpa cascade the delete operation of a location to the mappedRole table? 我如何才能让JPA级联将位置的删除操作映射到mappingRole表?

Cascading only works for the entity it is declared in. Ie, 级联仅适用于在其中声明的实体。即,

public class MappedRole {

  @ManyToOne(cascade = { CascadeType.REMOVE }, fetch = FetchType.EAGER)
  @JoinColumn(name = "TREENODE_ID", nullable = false)
  private TreeNode treeNode;

Should delete all corresponding TreeNode s if and when a MappedRole is deleted. 如果以及何时删除MappedRole删除所有对应的TreeNode

You don't seem to have the inverse @OneToMany relationship in TreeNode or Standort , so when you delete a TreeNode or Standort there's no relation on which JPA could cascade anything. 您似乎在TreeNodeStandort没有相反的@OneToMany关系,因此,当您删除TreeNodeStandort时,JPA可以级联任何东西都没有关系。

Try it like this: 像这样尝试:

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class TreeNode ... {

  @OneToMany(cascade= { CascadeType.REMOVE }, mappedBy="treeNode" )
  private List<MappedRole> mappedRoles;

and make sure to put each MappedRole also into the mappedRoles list of it's owning TreeNode . 并确保将每个MappedRole放入其拥有的TreeNodemappedRoles列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM