删除双向 OneToOne 关系中的子实体
delete child entity in bidirectional OneToOne relationship
我有以下 类 一段关系。
DrivingLicense.java
@Entity
@Table(name = "DRIVING_LICENSE")
public class DrivingLicense {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "LICENSE_NUMBER")
private int licenseNumber;
@Column(name = "DATE_OF_ISSUE")
private Date dateOfIssue;
@OneToOne
@JoinColumn(name = "PERSON_ID")
private Person person;
}
和
Person.java
@Entity
@Table(name = "PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PERSON_ID")
private int personId;
@Column(name = "PERSON_NAME", nullable = false, length = 30)
private String personName;
@OneToOne(mappedBy = "person", cascade = CascadeType.ALL)
private DrivingLicense drivingLicense;
}
现在当我尝试删除驾驶执照实体时却无法删除它,我很困惑,如何单独删除驾驶执照?
entityManager.getTransaction().begin();
DrivingLicense drivingLicense = entityManager.find(DrivingLicense.class, 6);
entityManager.remove(drivingLicense);
entityManager.getTransaction().commit();
更新
而不是在两次选择以下删除它的触发
Hibernate: select drivinglic0_.LICENSE_NUMBER as LICENSE_NUMBER1_0_0_, drivinglic0_.DATE_OF_ISSUE as DATE_OF_ISSUE2_0_0_, drivinglic0_.PERSON_ID as PERSON_ID3_0_0_, person1_.PERSON_ID as PERSON_ID1_1_1_, person1_.PERSON_NAME as PERSON_NAME2_1_1_ from DRIVING_LICENSE drivinglic0_ left outer join PERSON person1_ on drivinglic0_.PERSON_ID=person1_.PERSON_ID where drivinglic0_.LICENSE_NUMBER=?
Hibernate: select drivinglic0_.LICENSE_NUMBER as LICENSE_NUMBER1_0_1_, drivinglic0_.DATE_OF_ISSUE as DATE_OF_ISSUE2_0_1_, drivinglic0_.PERSON_ID as PERSON_ID3_0_1_, person1_.PERSON_ID as PERSON_ID1_1_0_, person1_.PERSON_NAME as PERSON_NAME2_1_0_ from DRIVING_LICENSE drivinglic0_ left outer join PERSON person1_ on drivinglic0_.PERSON_ID=person1_.PERSON_ID where drivinglic0_.PERSON_ID=?
为什么触发这 2 个选择?
首先尝试从其各自的 Person
中删除 drivingLicense
:
entityManager.getTransaction().begin();
DrivingLicense drivingLicense = entityManager.find(DrivingLicense.class, 6);
Person person = drivingLicense.getPerson();
person.setDrivingLicense(null);
entityManager.merge(person);
entityManager.remove(drivingLicense);
entityManager.getTransaction().commit();
我有以下 类 一段关系。
DrivingLicense.java
@Entity
@Table(name = "DRIVING_LICENSE")
public class DrivingLicense {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "LICENSE_NUMBER")
private int licenseNumber;
@Column(name = "DATE_OF_ISSUE")
private Date dateOfIssue;
@OneToOne
@JoinColumn(name = "PERSON_ID")
private Person person;
}
和
Person.java
@Entity
@Table(name = "PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PERSON_ID")
private int personId;
@Column(name = "PERSON_NAME", nullable = false, length = 30)
private String personName;
@OneToOne(mappedBy = "person", cascade = CascadeType.ALL)
private DrivingLicense drivingLicense;
}
现在当我尝试删除驾驶执照实体时却无法删除它,我很困惑,如何单独删除驾驶执照?
entityManager.getTransaction().begin();
DrivingLicense drivingLicense = entityManager.find(DrivingLicense.class, 6);
entityManager.remove(drivingLicense);
entityManager.getTransaction().commit();
更新
而不是在两次选择以下删除它的触发
Hibernate: select drivinglic0_.LICENSE_NUMBER as LICENSE_NUMBER1_0_0_, drivinglic0_.DATE_OF_ISSUE as DATE_OF_ISSUE2_0_0_, drivinglic0_.PERSON_ID as PERSON_ID3_0_0_, person1_.PERSON_ID as PERSON_ID1_1_1_, person1_.PERSON_NAME as PERSON_NAME2_1_1_ from DRIVING_LICENSE drivinglic0_ left outer join PERSON person1_ on drivinglic0_.PERSON_ID=person1_.PERSON_ID where drivinglic0_.LICENSE_NUMBER=?
Hibernate: select drivinglic0_.LICENSE_NUMBER as LICENSE_NUMBER1_0_1_, drivinglic0_.DATE_OF_ISSUE as DATE_OF_ISSUE2_0_1_, drivinglic0_.PERSON_ID as PERSON_ID3_0_1_, person1_.PERSON_ID as PERSON_ID1_1_0_, person1_.PERSON_NAME as PERSON_NAME2_1_0_ from DRIVING_LICENSE drivinglic0_ left outer join PERSON person1_ on drivinglic0_.PERSON_ID=person1_.PERSON_ID where drivinglic0_.PERSON_ID=?
为什么触发这 2 个选择?
首先尝试从其各自的 Person
中删除 drivingLicense
:
entityManager.getTransaction().begin();
DrivingLicense drivingLicense = entityManager.find(DrivingLicense.class, 6);
Person person = drivingLicense.getPerson();
person.setDrivingLicense(null);
entityManager.merge(person);
entityManager.remove(drivingLicense);
entityManager.getTransaction().commit();