使用复合外键对一对多进行hibernate注释

时间:2012-07-16 12:23:21

标签: java hibernate orm annotations

我对hibernate注释映射有很大的问题。继承我的图表:

diagram http://s13.postimage.org/vdye6yc8n/image.jpg

对于映射对象,很少有简单因为它的1 ... N为:
TASK_DEF -> REPORT_DATA
REPORT_DATA -> REPORT_DATA_COLUMN
REPORT_DATA -> REPORT_DATA_VALUE
正确?

现在问题是我正在尝试将REPORT_DATA_VALUE的集合映射到REPORT_DATA_COLUMN。我试过这种方式:

@OneToMany(fetch = FetchType.LAZY)
@ForeignKey(name = "FK_REPORT_DATA_VALUE_REPORT_DA", inverseName = "PK_REPORT_DATA_COLUMN")
@JoinTable(name = "REPORT_DATA_VALUE", joinColumns = {
        @JoinColumn(name = "REPORT_DATA_ID"),
        @JoinColumn(name = "COLUMN_NM")
}, inverseJoinColumns = {
        @JoinColumn(name = "REPORT_DATA_ID"),
        @JoinColumn(name = "COLUMN_NM")
})    
List<ReportDataValue> reportDataValueList;

但是hibernate选择了错误的标识,因此无法执行查询。有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:2)

您的代码存在问题。试试这个:

@OneToMany(fetch = FetchType.LAZY)
@JoinTable(name = "REPORT_DATA_VALUE", joinColumns = {
    @JoinColumn(name = "REPORT_DATA_ID"),
}, inverseJoinColumns = {
    @JoinColumn(name = "COLUMN_NM")
})    
List<ReportDataValue> reportDataValueList;

顺便说一句,如果我是你,我就不会为One To Many创建一张桌子。我会这样做:

public class A{
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="B_ID")
    B b;
}

public class B{
    @OneToMany(mappedBy="b",fetch=FetchType.EAGER)
    Set<A> as;
}