I want to join an order
table to with a different item table (book
or food
) based on the item_type
value. If item_type
is 0, item_id
should be from the book
table. If item_type
is 1, item_id
should be from the food
table.
Below are the sample tables. I hope they can help you understand my question.
create table order{
id int(11) unsigned NOT NULL AUTO_INCREMENT,
item_type int,
item_id int
}
create table book{
id int(11) unsigned NOT NULL AUTO_INCREMENT,
desc varchar(100)
}
create table food{
id int(11) unsigned NOT NULL AUTO_INCREMENT,
field1 varchar(100)
}
I have tried using the @wherejointable
annotaion.
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id",insert="false" update="false")
@WhereJoinTable(clause = "item_type=0")
public Book getBook() {
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id",insert="false" update="false")
@WhereJoinTable(clause = "item_type=1")
public Food getFood() {
}
However, I get the following error:
Repeated column in mapping for entity: column: item_id (should be mapped with insert="false" update="false")
Is this possible to achieve in hibernate?