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.
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
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With