Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pulling a field from another table in Hibernate

Tags:

java

hibernate

I have two tables, which have a one-to-one relationship and look something like this:

OrderItem

ID    data1   data2   data3
1     a       b       c
2     d       e       f
3     g       h       i

DisputedItem

ID    data4
1     q
3     r

Is there a way to pull data4 into my Hibernate model for OrderItem without having a separate DisputedItemModel? Preferably using annotations.

like image 918
Tin Wizard Avatar asked Jun 29 '15 17:06

Tin Wizard


2 Answers

You can use Hibernate @Formula annotation, if you have annotation based mappings or <formula> tag if you are mapping by XML.

What formula can do is help you extract a value by a query and map it to a field in your OrderItem domain model. This field can be from any table but the query thats used to map the property should return the indented field type.

You can use following URL for reference Hibernate Formula

like image 114
Johnson Abraham Avatar answered Nov 05 '22 05:11

Johnson Abraham


Yes, here is an example:

@Table(name = "table")
public class c
{
    @Id
    @Column(name = "ID")
    private int Id;

    @Formula("(select t.data from table t where t.ID = ID))")
    private String data;
}
like image 6
Seffy Golan Avatar answered Nov 05 '22 07:11

Seffy Golan