您好我正在尝试使用HSQLDB开发petclinic应用程序,Spring,Webflow& JSF。
BusinessEntities图层中的Vets和Specialties实体具有ManyToMany关系,如下所示。
Vets.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;
}
}
Specialties.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对象时,会使用setspecialtieses方法填充vets,special和vet_secialities表,但之前的关系会丢失,让我这样做。
添加ID为1的兽医
-------------- ----------------- --------------------
| 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 |
----------------- --------------------
这里我的先前条目被删除,新的存储被存储,我的要求是保留旧的vets条目。
我有vetsPage.xhtml,其中我有一个带有提交按钮的文本框和相应的vets.xml流,其中输入创建新的vetsVO对象并使用JPA将其保存在List中,此时Specialties设置为null并且在同一时间方式我有一个带有一个文本框的specialitiesPage和提交按钮以及相应的流程,它也创建了SpecialitiesVO对象并保存在列表中,现在我还有一个页面,即vetsSpecialitiesPage,我将从下拉列表中选择一个vetsVO对象并使用它映射特色并保存。通过这种方式,它可以妥善保存,但会严重影响现有关 尝试了几个建议,即使用mappedBy,CascadeType.MERGE等..但没有给出理想的结果。感谢任何帮助。