组合在Hibernate中如何工作? [英] How does composition work in Hibernate?

查看:65
本文介绍了组合在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆