【问题标题】:Java JPA - OneToOne Relation between Entities not workingJava JPA - 实体之间的 OneToOne 关系不起作用
【发布时间】:2012-01-06 15:19:04
【问题描述】:

合法实体:

@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;

    ...
}

数据库:

诉讼:

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

产品:

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

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

我做错了什么?

* 编辑 *

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

{IndirectSet: 未实例化}

【问题讨论】:

  • 您可能想看看 Embeddable annotation...
  • 除了 MappedSuperClass 注释外,我没有看到任何错误。您确定每个 licitatie 都有自己的产品(因为查询会返回数据库中的每个citatie)? licitatie_id 列是否有唯一约束? JPA 引擎是什么?
  • 我确信每个“licitatie”只有一个“produs”记录。

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


【解决方案1】:

已解决

我已将 PRODUS_ID 列添加到 licitatie 表中。

Licitatie: id produs_id ... 产品:id, licitatie_id ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    相关资源
    最近更新 更多