问题描述
我有两个实体A和B.
public class A {
@Id
@GeneratedValue
私人整数ID;
私人字符串uuid;
...
}
UUID由外部给出; ID可以看作是版本。
现在,我想引用B中的A,这样我就可以将存储在B中的uuid自动选择
public class B {
@Id
@GeneratedValue
private Integer id;
私人字符串uuidOfA;
@ManyToOne
@JoinFormula(value =SELECT a.id FROM A WHERE v.uuid = uuidOfA AND v.id =(SELECT max(x.id)FROM x WHERE x.uuid = v.uuid),referencedColumnName =id)
private A a;
...
}
这将创建一列在B中包含A的id,如果我尝试持久化一个对象,则会引发异常。我也试过@JoinColumnsOrFormulas,但没有幸运。
有人可以告诉我如何做到这一点(在Hibernate 3.5 btw中)?
谢谢!
@ManyToOne
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula = @ JoinFormula(value =(SELECT a.id FROM A a WHERE a.uuid = $ uid),referencedColumnName =id)),
@JoinColumnOrFormula(column = @JoinColumn(uuidOfA,referencedColumnName =uuid))
})
private A a;
I have two entities A and B.
public class A{
@Id
@GeneratedValue
private Integer id;
private String uuid;
...
}
The UUID is given externally; ID can be seen as version.
Now, I'd like to reference A in B such that I have the uuid stored in B and automatically select the A with the according uuid and the highest id.
What I tried is:
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's id in B and throws an exception if I try to persist an object. I've also tried @JoinColumnsOrFormulas without luck.
Can someone give me a hint on how to do this (in Hibernate 3.5 btw)?
Thanks!
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;
这篇关于Hibernate @JoinFormula的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!