Inheritance 如何使用级联删除删除JPA中的继承类?

Inheritance 如何使用级联删除删除JPA中的继承类?,inheritance,jpa,annotations,eclipselink,cascading-deletes,Inheritance,Jpa,Annotations,Eclipselink,Cascading Deletes,这个问题可能有点让人困惑,所以我会尝试用一些例子来说明 我正在使用JPA2.0和Eclipselink 2.2 我有三门课,个人,学生和证书,它们之间有这种关系: 学生从Person继承(扩展) 学生有证书(@OneToOne) 我的类定义如下: Person.java @Entity @Table(name = "person") @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(name = "typ

这个问题可能有点让人困惑,所以我会尝试用一些例子来说明

我正在使用JPA2.0和Eclipselink 2.2

我有三门课,个人学生证书,它们之间有这种关系:

  • 学生从Person继承(扩展
  • 学生有证书(@OneToOne
我的类定义如下:

Person.java

@Entity
@Table(name = "person")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, length = 20)
@DiscriminatorValue("person")
public abstract class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String firstName;
    private String lastName;
    private String street;
    private String city;
    private String zipCode;
    private String phoneNumber;

    /*Getters and Setters*/

}
@SuppressWarnings("serial")
@Entity
@Table(name = "student")
@DiscriminatorValue("student")
public class Student extends Person implements Serializable {

    @Column(unique = true)
    private String studentId;

    @OneToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true, optional = false, mappedBy = "student")
    @CascadeOnDelete
    private Credentials credentials;

    /*Getters and Setters*/
}
@SuppressWarnings("serial")
@Entity
public class Credentials implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(unique = true)
    private String username;

    private String password;

    private Student student;
}
Student.java

@Entity
@Table(name = "person")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, length = 20)
@DiscriminatorValue("person")
public abstract class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String firstName;
    private String lastName;
    private String street;
    private String city;
    private String zipCode;
    private String phoneNumber;

    /*Getters and Setters*/

}
@SuppressWarnings("serial")
@Entity
@Table(name = "student")
@DiscriminatorValue("student")
public class Student extends Person implements Serializable {

    @Column(unique = true)
    private String studentId;

    @OneToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true, optional = false, mappedBy = "student")
    @CascadeOnDelete
    private Credentials credentials;

    /*Getters and Setters*/
}
@SuppressWarnings("serial")
@Entity
public class Credentials implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(unique = true)
    private String username;

    private String password;

    private Student student;
}
Credentials.java

@Entity
@Table(name = "person")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, length = 20)
@DiscriminatorValue("person")
public abstract class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String firstName;
    private String lastName;
    private String street;
    private String city;
    private String zipCode;
    private String phoneNumber;

    /*Getters and Setters*/

}
@SuppressWarnings("serial")
@Entity
@Table(name = "student")
@DiscriminatorValue("student")
public class Student extends Person implements Serializable {

    @Column(unique = true)
    private String studentId;

    @OneToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true, optional = false, mappedBy = "student")
    @CascadeOnDelete
    private Credentials credentials;

    /*Getters and Setters*/
}
@SuppressWarnings("serial")
@Entity
public class Credentials implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(unique = true)
    private String username;

    private String password;

    private Student student;
}
问题是当我试图删除一个学生时

        entityManager.getTransaction().begin();
        entityManager.remove(student);
        entityManager.getTransaction().commit();
出现此约束(我可以发布更多错误消息,但这似乎是关键):

我已经尝试过几次助教,一个方向,学生和证书之间的双向关系,但结果几乎都是一样的

如何删除学生实体


非常感谢您提供的任何帮助。

不回答您的问题,但由于Person类中没有关系,因此您可以使用mappedSuperClass而不是InherInstance(以获得更好的性能)因为Person是抽象的:我看不出有任何理由在Person类中定义鉴别器值。只有在数据库中的关系上设置了cascade delete时,才能使用cascade delete注释。设置它意味着当数据库删除学生时,它还将负责删除凭据。由于没有发生这种情况,您可能没有在数据库中设置级联删除。删除注释,JPA将为您执行删除操作,或者更改数据库-您可以让EclipseLink DDL删除并为您重新创建数据库EclipseLink 2.4我相信indluce会在生成的DDL@ben75很抱歉,在尝试做一些测试时,抽象描述被放在了后面。我也尝试过
@MappedSuperclass
,但得到了相同的结果。@Chris我的数据库完全是通过persistence.xml文件生成的。我放置了一些伪代码,并使用
语法在开发过程中重建结构。我没有运气手动删除这些表。