您好我正在尝试使用 HSQLDB、Spring、Webflow 和 JSF 开发宠物诊所应用程序。
其中BusinessEntities 层中的Vets 和Specialties 实体具有ManyToMany 关系,如下所示。
兽医.java
@Entity
@Table(name = "VETS", schema = "PUBLIC", catalog = "PUBLIC")
public class Vets implements BaseEntity, java.io.Serializable {
private Long id;
private String firstName;
private String lastName;
private Set<Specialties> specialtieses = new HashSet<Specialties>(0);
public Vets() {
}
public Vets(Long id) {
this.id = id;
}
public Vets(Long id, String firstName, String lastName,
Set<Specialties> specialtieses) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.specialtieses = specialtieses;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "FIRST_NAME", length = 30)
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "LAST_NAME", length = 30)
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "VET_SPECIALTIES", schema = "PUBLIC", catalog = "PUBLIC", joinColumns = { @JoinColumn(name = "VET_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "SPECIALTY_ID", nullable = false, updatable = false) })
public Set<Specialties> getSpecialtieses() {
return this.specialtieses;
}
public void setSpecialtieses(Set<Specialties> specialtieses) {
this.specialtieses = specialtieses;
}
}
专业.java
@Entity
@Table(name = "SPECIALTIES", schema = "PUBLIC", catalog = "PUBLIC")
public class Specialties implements BaseEntity, java.io.Serializable {
private Long id;
private String name;
private Set<Vets> vetses = new HashSet<Vets>(0);
public Specialties() {
}
public Specialties(Long id) {
this.id = id;
}
public Specialties(Long id, String name, Set<Vets> vetses) {
this.id = id;
this.name = name;
this.vetses = vetses;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "NAME", length = 80)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "VET_SPECIALTIES", schema = "PUBLIC", catalog = "PUBLIC", joinColumns = { @JoinColumn(name = "SPECIALTY_ID", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "VET_ID", nullable = false, updatable = false) })
public Set<Vets> getVetses() {
return this.vetses;
}
public void setVetses(Set<Vets> vetses) {
this.vetses = vetses;
}
}
当我使用 setSpecialtieses 方法添加具有专业的 vets 对象时,填充了 vets、specialities 和 vet_secialities 表,但以前的关系正在丢失,让我这样说。
添加 ID 为 1 的 vet
-------------- ----------------- --------------------
| Vets | | Specialities | | vet_specialities |
| --------------| |-----------------| |--------------------|
| ID Name | | ID Name | |Spe_ID Vet_ID |
| --------------| |-----------------| |--------------------|
| 1 ABC | | 1 Surgery | | 1 1 |
-------------- | 2 Radiology | | 2 1 |
----------------- --------------------
当我添加另一个 ID 为 2 的 Vet 对象时,
-------------- ----------------- --------------------
| Vets | | Specialities | | vet_specialities |
| --------------| |-----------------| |--------------------|
| ID Name | | ID Name | |Spe_ID Vet_ID |
| --------------| |-----------------| |--------------------|
| 2 ABC | | 1 Surgery | | 1 2 |
-------------- | 2 Radiology | | 2 2 |
----------------- --------------------
在这里,我以前的条目被删除并存储了新条目,我的要求是也保留旧的兽医条目。
我有 vetsPage.xhtml,其中我有一个带有提交按钮的文本框和相应的 vets.xml 流,其条目创建新的 vetsVO 对象并使用 JPA 将其保存在 List 中,此时 Specialties 集为空,我有同样的方式specialitiesPage 带有一个文本框和提交按钮以及相应的流程,该流程还创建 SpecialitiesVO 对象并保存在 List 中,现在我还有一个页面,即 vetsSpecialitiesPage,我将从下拉列表中选择一个 vetsVO 对象并使用它映射专业并保存。通过这种方式,它可以适当地保存,但会覆盖现有的关系。尝试了几个建议,即使用 mappedBy、CascadeType.MERGE 等。但没有一个给出预期的结果。感谢任何帮助。