问题描述
在spring mvc应用程序中,我在Caregiver
实体和Patient
实体之间具有多对多关系.创建新caregiver
的jsp视图未将指定的patient
添加到与caregiver
实例关联的patients
集合中.您可以在下面的控制器代码打印屏幕中看到此代码,该代码正在调试模式下运行,并已停止在应将patient
添加到caregiver
的patients
集合中的点下方的行上:
In a spring mvc app, I have a many to many relationship between a Caregiver
entity and a Patient
entity. The jsp view that creates a new caregiver
is not adding the specified patient
to the patients
collection that is associated with the caregiver
instance. You can see this in the following printscreen of the controller code, which is being run in debug mode and has stopped at the line below the point where the patient
should have been added to the caregiver
's collection of patients
:
类似地,下面的下一个打印屏幕显示调试器,该调试器指示在给定的caregiver
内patients
集合为空,在上面指示的控制器执行中,应将patient
添加到caregiver
:
Similarly, the next printscreen below shows the debugger indicating that the patients
collection is empty for the given caregiver
, at the point in the controller's execution indicated above, where the patient
should have been added to the caregiver
:
基础mysql数据库中的查询表明caregiver
已添加到caregiver
表中,但是联接表中没有记录应将caregiver
与指定的patient
链接起来.为什么patient
没有添加到caregiver
中?
Queries in the underlying mysql database show that the caregiver
has been added to the caregiver
table, but that there is no record in the join table that should link the caregiver
with the specified patient
. Why is the patient
not added to the caregiver
?
如果我在代码的那部分写了patient.addCaregiver(caregiver)
或caregiver.addPatient(patient)
,结果是堆栈溢出错误,并且不断重复地添加caregiver
和patient
,这表明手动添加显然是不必要的.那么,如何确保添加patient
?
If I write patient.addCaregiver(caregiver)
or caregiver.addPatient(patient)
in that part of the code, the result is a stack overflow error with endless repetitive adding of caregiver
and patient
to each other, indicating that adding manually is apparently unnecessary. So how do I make sure the patient
gets added?
作为参考,我已经将Caregiver
实体和Patient
实体的完整代码添加到了文件共享站点,您可以通过单击以下链接来访问该站点:
For reference, I have added the full code of the Caregiver
entity and the Patient
entity to a file sharing site, which you can access by clicking the following links:
单击此处以查看Caregiver
实体的代码.
您可以通过单击以下链接来查看Patient
实体的代码 .
Click here to view the code for the Caregiver
entity.
You can view the code for the Patient
entity by clicking on this link.
按照Kresimir的建议,我如下更改了Patient.addCaregiver()方法:
Following Kresimir's advice, I altered the Patient.addCaregiver() method as follows:
public void addCaregiver(Caregiver c) {getCaregiversInternal().add(c);}
我还将Caregiver.addPatient()方法修改为:
I also modified the Caregiver.addPatient() method to be:
public void addPatient(Patient p) {getPatientsInternal().add(p);}
请注意,如上面链接中文件共享站点上的代码所示,getCaregiversInternal()和getPatientsInternal()分别返回看护者和患者的集合.
Note that getCaregiversInternal() and getPatientsInternal() return the caregivers and patients collections, respectively, as shown in the code at the file sharing site in the links above.
新护理人员仍未与相应患者的病历相关联.我如何特别地更改这些或其他方法以实现此目的?
New caregivers are still not associated to the respective patient's collection. How specifically do I change these or other methods to make this happen?
推荐答案
从映射来看,Patient
是拥有Patient
-Caregiver
关系的实体.可以从inverseJoinColumn
指向caregiver_id
的事实推论得出.
Judging from the mapping, Patient
is the entity that owns the Patient
- Caregiver
relationship. This can be deduced from the fact inverseJoinColumn
points to caregiver_id
.
@ManyToMany(cascade = {CascadeType.ALL}, fetch=FetchType.EAGER)
@JoinTable(name="careTeamJunction",
joinColumns={@JoinColumn(name="patient_id")},
inverseJoinColumns={@JoinColumn(name="caregiver_id")}) //patient owns relationship
private Set<Caregiver> caregivers = new HashSet<Caregiver>();
由于Patient
是拥有关系的实体,因此将Caregiver
添加到Patient
对象的caregivers
集中就足够了,并且hibernate应该将关系存储在联接表中.
Since Patient
is the entity that owns the relationship, it should be enough to add Caregiver
to Patient
object's caregivers
set and hibernate should store the relationship in the join table.
此外,addPatient和addCaregiver方法也存在问题-它们互相调用,从而创建了无限递归.修改Patient.addCaregiver以将看护者添加到患者的看护者集中,但不要调用caregiver.addPatient以避免递归(或者在caregiver.addPatient中删除对Patient.addCaregiver的调用).
Also, there is a problem with addPatient and addCaregiver methods - they call each other creating an endless recursion. Modify patient.addCaregiver to add caregiver to patient's caregiver set but do not call caregiver.addPatient also to avoid recursion (alternatively in caregiver.addPatient remove call to patient.addCaregiver).
这篇关于集合没有以多对多关系填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!