为什么用于同步实体关系两端的辅助方法位于父实体中?
Why helper method for synchronizing both ends of the entity relationship locates in parent entity?
在Hibernate guide and this blog中,父实体(关系的反面)具有用于同步实体关系两端的辅助方法。
父实体的 Hibernate 指南:
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
private Long id;
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Phone> phones = new ArrayList<>();
//Getters and setters are omitted for brevity
public void addPhone(Phone phone) {
phones.add( phone );
phone.setPerson( this );
}
public void removePhone(Phone phone) {
phones.remove( phone );
phone.setPerson( null );
}
}
子实体的 Hibernate 指南:
@Entity(name = "Phone")
public static class Phone {
@Id
@GeneratedValue
private Long id;
@NaturalId
@Column(name = "`number`", unique = true)
private String number;
@ManyToOne
private Person person;
//Getters and setters are omitted for brevity
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
Phone phone = (Phone) o;
return Objects.equals( number, phone.number );
}
@Override
public int hashCode() {
return Objects.hash( number );
}
}
为什么我们不需要子实体中的辅助方法来确保两个实体像下面这样同步?
@Entity(name = "Phone")
public static class Phone {
@Id
@GeneratedValue
private Long id;
@NaturalId
@Column(name = "`number`", unique = true)
private String number;
@ManyToOne
private Person person;
//Getters and setters are omitted for brevity
public void addPerson(Person person) {
setPerson( person );
person.getPhones().add( this );
}
public void removePerson(Person person) {
setPerson( null );
person.getPhones().remove( this );
}
}
他们只是展示了这个概念。从子实体同步关系是完全有效的,只要您可以确保它们的关系都已配置 属性.
在Hibernate guide and this blog中,父实体(关系的反面)具有用于同步实体关系两端的辅助方法。
父实体的 Hibernate 指南:
@Entity(name = "Person")
public static class Person {
@Id
@GeneratedValue
private Long id;
@OneToMany(mappedBy = "person", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Phone> phones = new ArrayList<>();
//Getters and setters are omitted for brevity
public void addPhone(Phone phone) {
phones.add( phone );
phone.setPerson( this );
}
public void removePhone(Phone phone) {
phones.remove( phone );
phone.setPerson( null );
}
}
子实体的 Hibernate 指南:
@Entity(name = "Phone")
public static class Phone {
@Id
@GeneratedValue
private Long id;
@NaturalId
@Column(name = "`number`", unique = true)
private String number;
@ManyToOne
private Person person;
//Getters and setters are omitted for brevity
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
Phone phone = (Phone) o;
return Objects.equals( number, phone.number );
}
@Override
public int hashCode() {
return Objects.hash( number );
}
}
为什么我们不需要子实体中的辅助方法来确保两个实体像下面这样同步?
@Entity(name = "Phone")
public static class Phone {
@Id
@GeneratedValue
private Long id;
@NaturalId
@Column(name = "`number`", unique = true)
private String number;
@ManyToOne
private Person person;
//Getters and setters are omitted for brevity
public void addPerson(Person person) {
setPerson( person );
person.getPhones().add( this );
}
public void removePerson(Person person) {
setPerson( null );
person.getPhones().remove( this );
}
}
他们只是展示了这个概念。从子实体同步关系是完全有效的,只要您可以确保它们的关系都已配置 属性.