【问题标题】:Hibernate OneToMany relationship: not mapped to a single properyHibernate OneToMany 关系:未映射到单个属性
【发布时间】:2016-09-12 10:57:44
【问题描述】:

我有两个带有这些主键的表:

  TABLE A                 TABLE B
  ----------             ----------             
 | colA     |----->     | colX     |
 | colB     |----->     | colY     |
 | colC     |----->     | colW     |
 |__________|           | colZ     |
                        |__________|

基本上我需要在 JPA 1.0 中定义这种关系。

我尝试使用以下代码映射 tableA 的实体:

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, targetEntity=TableB.class)
    @JoinColumns({      
            @JoinColumn(name="colX", referencedColumnName="colA", insertable=false, updatable=false),
            @JoinColumn(name="colY", referencedColumnName="colB", insertable=false, updatable=false),
            @JoinColumn(name="colW", referencedColumnName="colC", insertable=false, updatable=false)
        })      

private Set<TableB> tableB;

..get and set

我得到的只是这个错误:

    org.hibernate.AnnotationException: Unable to map collection TableB

    Caused by: org.hibernate.AnnotationException: referencedColumnNames(colA, colB, colC) of tableB referencing tableA not mapped to a single property

有什么帮助吗?

编辑*

表 A 和表 B 都获得了 @EmbeddedId 主键类,并在顶部声明了它们自己的 pk cols。

下面的代码更好地解释了这种情况

// TABLE A PKey Entity

@Embeddable
class TableAPKey 
{
    @Column
    String colA; // get and set
    @Column
    String colB; // get and set
    @Column
    String colC; // get and set
}   

// TABLE A Entity

class TableA
{
    @EmbeddedId
    TableAPKey key; // get and set
}
// TABLE B PKey entity

@Embeddable
class TableBPKey 
{
    @Column
    String colX; // get and set
    @Column
    String colY; // get and set
    @Column
    String colW; // get and set
    @Column
    String colZ; // get and set NOT USED IN RELATIONSHIP with TableA
}   

// TABLE B Entity

class TableB
{
    @EmbeddedId
    TableBPKey key; // get and set
}

【问题讨论】:

  • colAcolBcolCTableA的复合主键吗?请发布TableA实体的ID字段。
  • 我通过插入实体 A 和 B 的定义改进了问题
  • B和A有关系吗?

标签: java hibernate jpa one-to-many hibernate-onetomany


【解决方案1】:

您的映射部分不正确。请尝试以下操作:

class TableA {

    // ...
    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, targetEntity=TableB.class, mappedBy = "key.tableA")
    private Set<TableB> tableB;
}


@Embeddable
class TableBPKey {

    @ManyToOne
    @JoinColumns({
        @JoinColumn(name = "colX", referencedColumnName = "colA"),
        @JoinColumn(name = "colY", referencedColumnName = "colB")
        @JoinColumn(name = "colW", referencedColumnName = "colC")
    })        
    private TableA tableA;

    @Column
    String colZ;

    // ...
}   

【讨论】:

  • 执行“关键”。指的是tableA还是tableB的主键?
  • 当您引用 Set&lt;TableB&gt; 时,它的意思是 keyTableB
  • 仍然返回同样的错误“referencedColumnNames not mapped to a single property”
  • 奇怪,你在TableA上真的没有@JoinColumns了吗?
  • 表示你在referencedColumnName中提到的一列在目标实体中没有找到。
猜你喜欢
  • 2012-02-08
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 2015-05-30
相关资源
最近更新 更多