EJB继承策略

时间:2013-01-27 17:25:47

标签: jpa ejb

BIG UPDATE:

好的,我看到我的问题比我想象的复杂得多。我有这样的表:

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中的主键重复值(但是我在字段Account中使用TableGenerator ...)或NULL值但是对于INSERT INTO Doctor(如何?!我正在创建新的Patient,而不是Doctor ...)。我现在完全迷失了,所以我认为现在对我来说最重要的是知道在这种情况下我是否可以使用InheritanceType.JOINED

3 个答案:

答案 0 :(得分:1)

更新:

您使用字段nazwa作为鉴别器

@DiscriminatorColumn(discriminatorType=DiscriminatorType.STRING,name="nazwa")

框架存储了它必须用于反序列化对象的类的名称(它是PoziomDostepu类的名称)。

据我所知,你为每个班级使用不同的表格,所以Strategy.JOINED没什么意义。

更新更新:

我说class的意思是entity。您可以通过更改PoziomDostepu的实体名称(例如“CacaCuloPedoPis”)并查看插入的新值来检查效果。

答案 1 :(得分:0)

看起来所有缺失的是PoziomDostepu类上的@DiscriminatorValue注释,以使用约束中的一个值,否则默认为类名。 PoziomDostepu的插入来自this.poziom - 如果你不想要插入PoziomDostepu实例,你可以使类抽象并确保这个实例是一个子类。您不应该更改继承类型,因为这样做需要将数据库表更改为包含相同字段的数据库表。

答案 2 :(得分:0)

好的,我做了我想做的事。它不优雅,但效率很高。表和继承策略是相同的。在端点我创建帐户和人员实体,然后我创建 Patient 实体,但使用EntityManager persist()方法。我还需要手动设置级别的id ((Integer)poziomfacade.getEntityManager().createQuery("SELECT max(p.idpoziom) FROM PoziomDostepu p").getSingleResult())+1,因为 Level 实体类中的Generator不起作用。然而,问题已经解决,感谢每一个建设性的评论或答案。

相关问题