开发者

Many-to-one with formula in Hibernate

开发者 https://www.devze.com 2023-03-12 06:23 出处:网络
I have the following tables: tableA: id[PK], name tableB: id[PK], idTableA[FK], dateClosed And my object model is the following:

I have the following tables:

tableA: id[PK], name
tableB: id[PK], idTableA[FK], dateClosed

And my object model is the following:

class ObjectA {
    private Integer id:
    private String name;
    private ObjectB activeB;
}

class ObjectB {
    private Integer id;
    private ObjectA a;
    private Date dateClosed;
}

So the property activeB in ObjectA should be the one whose dateClosed is NULL (there can be only one ObjectB at the time with null dateClosed).

I'm stuck wh开发者_StackOverflow中文版ile writing the many-to-one relation with the correct formula to get the active ObjectB... Thanks!


It seems it's actually a one-to-one relationship between A and B.

Below is Java EE 6; I don't know if there is much difference with hibernate.

@Entity    
class ObjectA {
    @Id @GeneratedValue
    private Integer id:
    private String name;
    @OneToOne(mappedBy = "a")
    private ObjectB activeB;
}

@Entity
class ObjectB {
    @Id @GeneratedValue
    private Integer id;
    @OneToOne
    @JoinColumn(name = "idTableA")
    private ObjectA a;
    private Date dateClosed;
}

As far as having a single B with null value, my guess is that it cannot be implemented in a database. So you would need the rest of your code (non-relational) to take care of this aspect.

0

精彩评论

暂无评论...
验证码 换一张
取 消