组合在Hibernate中如何工作? [英] How does composition work in Hibernate?
问题描述
我正在尝试在带注释的休眠状态下使用合成。
I'm trying to use composition in hibernate with annotations.
我有:
@Entity
@Table(name = "Foo")
public class Foo {
private Bar bar;
public void setBar(Bar bar){...}
public Bar getBar() {...)
}
public class Bar {
private double x;
public void setX(double x) {...}
public double getX() {...}
}
当试图保存Foo时,我得到了
And when trying to save Foo, I'm getting
无法确定表Foo上实体
org.bla.Bar的类型:
[org.hibernate.mapping.Column(bar)]
Could not determine type for entity org.bla.Bar at table Foo for columns: [org.hibernate.mapping.Column(bar)]
我尝试在Bar上放置@Entity批注,但这使我:
I tried putting an @Entity annotation on Bar, but this gets me:
没有标识符指定给实体
org.bla.Bar
No identifier specified for entity org.bla.Bar
推荐答案
参考文档的这一部分:
显然,hibernate为此使用JPA注释,因此解决方案拉尔夫所指的是正确的
Apparently hibernate uses JPA annotations for this purpose, so the solution referred to by Ralph is correct
如果将课程地址
标记为 @Embeddable
并将类型为 Address
的属性添加到类 User
中,将该属性标记为 @Embedded
,那么生成的数据库表 User
将具有 Address
指定的所有字段。
if you mark a class Address
as @Embeddable
and add a property of type Address
to class User
, marking the property as @Embedded
, then the resulting database table User
will have all fields specified by Address
.
有关代码,请参见拉尔夫的答案。
See Ralph's answer for the code.
这篇关于组合在Hibernate中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!