kamil
kamil

Reputation: 3522

hibernate annotations one to many with composite foreign key

I have quite big problem with hibernate annotation mapping. Heres my diagram:

diagram

As for mapping object, few are simple cause its 1...N for:
TASK_DEF -> REPORT_DATA
REPORT_DATA -> REPORT_DATA_COLUMN
REPORT_DATA -> REPORT_DATA_VALUE
right?

Now problem is when I'm trying to map Collection of REPORT_DATA_VALUE to REPORT_DATA_COLUMN. I've tried this way:

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

But hibernate selects wrong identifies, therefore couldnt execute query. Could someone help me with this?

Upvotes: 2

Views: 2356

Answers (1)

Matin Kh
Matin Kh

Reputation: 5178

There is a problem in your code. Try this instead:

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

By the way, If I were you, I wouldn't create a table for One To Many. I'd do this:

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

Upvotes: 2

Related Questions