Hibernate @JoinFormula

I have two objects A and B.

public class A{ @Id @GeneratedValue private Integer id; private String uuid; ... } 

UUID is provided externally; ID can be seen as version.

Now I would like to refer to A in B, so that I have uuid in B and automatically select A with the corresponding uuid and highest id.

What I tried:

 public class B{ @Id @GeneratedValue private Integer id; private String uuidOfA; @ManyToOne @JoinFormula(value="SELECT a.id FROM A a WHERE v.uuid = uuidOfA AND v.id = (SELECT max(x.id) FROM A x WHERE x.uuid = v.uuid)", referencedColumnName="id") private A a; ... } 

This will create a column containing A id in B and throw an exception if I try to save an object. I also tried @JoinColumnsOrFormulas with no luck.

Can someone give me a hint on how to do this (in Hibernate 3.5 battles)?

Thanks!

+6
source share
1 answer

The following works:

 @ManyToOne @JoinColumnsOrFormulas({ @JoinColumnOrFormula( formula=@JoinFormula (value="(SELECT a.id FROM A a WHERE a.uuid = uuid)", referencedColumnName="id")), @JoinColumnOrFormula(column = @JoinColumn("uuidOfA", referencedColumnName="uuid")) }) private A a; 
+9
source

Source: https://habr.com/ru/post/917794/


All Articles