【发布时间】:2013-01-27 17:25:46
【问题描述】:
重大更新:
好的,我发现我的问题比我想象的要复杂得多。我有这样的表:
Patient:
id
bloodtype
level (FK to level table)
doctor (FK to doctor table)
person (FK to person table)
Doctor:
id
person (FK)
speciality
level (FK to level)
Paramedic:
id
person (FK)
Person:
id
name
surname
Account:
id
login
pass
Level:
id
account (FK)
name (one of: 'patient' , 'paramedic' , 'doctor')
在实体类中,我现在在类Level 中使用@Inheritance(strategy= InheritanceType.JOINED)
@DiscriminatorColumn(discriminatorType=DiscriminatorType.STRING,name="name")。检查某人是否是前任。患者我有功能:
public boolean ifPatient(String login) {
account = accountfacade.getByLogin(login);
for (Level l : account.getLevelCollection()) {
if (l instanceof Patient) {
return true;
}
}
return false;
}
现在我的情况是:我以医生身份登录。我想添加一个病人。我有这样的事情:
public Doctor findDoctor(String login) {
account = accountfacade.getByLogin(login);
for (Doctor d : doctorfacade.findAll()) {
if (d.getLevel().getAccount().getLogin().equals(login)) {
doctor = d;
return doctor;
}
}
}
@Override
public void addPatient(Account acc, Patient pat, Person per, String login) {
Doctor d = findDoctor(login);
accountfacade.create(acc);
personfacade.create(per);
Patient p = new Patient();
p.setAccount(acc);
p.setBlood(pat.getBlood());
// etc
p.setPerson(per);
d.getPatientCollection().add(p);
acc.getLevelCollection().add(p);
}
但它不起作用。总是完全奇怪的错误,例如表 Account 中主键的重复值(但我使用 TableGenerator...)或字段 Account 中的 NULL 值但对于 INSERT INTO Doctor(如何?!我正在创建新的 Patient,而不是 Doctor.. .)。我现在完全迷路了,所以我认为现在对我来说最重要的是知道在这种情况下我是否真的可以使用InheritanceType.JOINED。
【问题讨论】:
-
1) 收到答案后不要完全更改问题,创建一个新问题。 2)停止考虑表格,考虑实体。 3)一旦 2 完成,继承策略与操作无关(根据性能/表的大小/表的数量选择一个或另一个可能会很有趣,但除此之外,当你在使用实体)。