Java JPA - 实体之间的OneToOne关系不起作用

时间:2011-11-25 21:12:45

标签: java jpa entity nullpointerexception one-to-one

Licitatie实体:

@Entity
@Table(name="licitatie")
public class Licitatie implements Serializable {
    ...

    //bi-directional one-to-one association to Licitatie
    @OneToOne(mappedBy="licitatie")
    private Produs produs;

    ...
}

Produs实体:

@Entity
@Table(name="produs")
//@DiscriminatorColumn(name="id", discriminatorType=DiscriminatorType.INTEGER)
@MappedSuperclass
public class Produs implements Serializable {
    ...

    //bi-directional one-to-one association to Produs
    @OneToOne
    @JoinColumn(name="licitatie_id")
    private Licitatie licitatie;

    ...
}

数据库:

Licitatie:

id  start   status
1   5   open
2   5   open
3   5   open

Produs:

id  licitatie_id    DTYPE   description
1   1           Carte   ...
2   2           Carte   ...
3   3           Carte   ...

运行tris查询后:“SELECT t FROM Licitatie t”,来自Licitatie类型的对象的属性Product为null。但是表格中有记录。

我做错了什么?

*编辑 *

从表中获取数据后,调用getProdus()会返回以下消息:

{IndirectSet:not instantiated}

1 个答案:

答案 0 :(得分:0)

解决

我已将列PRODUS_ID添加到licitatie表。

Licitatie:id produs_id ...... Produs:id,licitatie_id ......