Hibernate 中的组合如何工作?

发布于 2024-10-05 07:00:54 字数 550 浏览 2 评论 0 原文

我正在尝试在休眠中使用带有注释的组合。

我有:

@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 时,我得到了

无法确定实体的类型 org.bla.Bar 位于表 Foo 的列: [org.hibernate.mapping.Column(bar)]

我尝试在 Bar 上添加 @Entity 注释,但这让我感到困惑:

没有为实体指定标识符 org.bla.Bar

I'm trying to use composition in hibernate with annotations.

I have:

@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() {...}
}

And when trying to save Foo, I'm getting

Could not determine type for entity
org.bla.Bar at table Foo for columns:
[org.hibernate.mapping.Column(bar)]

I tried putting an @Entity annotation on Bar, but this gets me:

No identifier specified for entity
org.bla.Bar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人问我粥可暖 2024-10-12 07:00:54

参考文档的本节描述了该机制:

5.1.5。嵌入式对象(又名组件)

显然,hibernate 使用 JPA 注释来实现此目的,因此解决方案 Ralph 引用的内容是正确的

简而言之:

如果将类 Address 标记为 @Embeddable 并添加类型 < 的属性code>Address 到类 User,将属性标记为 @Embedded,然后生成的数据库表 User 将指定所有字段通过地址

请参阅拉尔夫的答案以获取代码。

The mechanism is described in this section of the reference docs:

5.1.5. Embedded objects (aka components)

Apparently hibernate uses JPA annotations for this purpose, so the solution referred to by Ralph is correct

In a nutshell:

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.

眼眸里的快感 2024-10-12 07:00:54

您需要指定 FooBar 之间的关系(使用 @ManyToOne 或 @OneToOne 之类的东西)。

或者,如果 Bar 不是实体,则用 @Embeddable 标记它,并将 @Embedded 添加到 Foo 中的变量声明中。

@Entity
@Table(name = "Foo")
public class Foo {
    @Embedded
    private Bar bar;

    public void setBar(Bar bar){...}
    public Bar getBar() {...)
}

@Embeddable
public class Bar {
  private double x;

  public void setX(double x) {...}
  public double getX() {...}
}

请参阅:https://www.baeldung.com/jpa-embedded-embeddable - - 该示例解释了@Embeddable和@Embedded Composite方式,其中FooBarCompanyContactPerson在示例)映射在同一个表中。

You need to specifiy the relationship between Foo and Bar (with something like @ManyToOne or @OneToOne).

Alternatively, if Bar is not an Entity, then mark it with @Embeddable, and add @Embedded to the variable declaration in Foo.

@Entity
@Table(name = "Foo")
public class Foo {
    @Embedded
    private Bar bar;

    public void setBar(Bar bar){...}
    public Bar getBar() {...)
}

@Embeddable
public class Bar {
  private double x;

  public void setX(double x) {...}
  public double getX() {...}
}

See: https://www.baeldung.com/jpa-embedded-embeddable -- The example expains the @Embeddable and @Embedded Composite way, where Foo and Bar (Company and ContactPerson in the example) are mapped in the same Table.

小女人ら 2024-10-12 07:00:54

每个简单实体必须标记为实体(使用@Entity)并具有一个标识符(主要是长整型)作为主键。每个非原始关联/组合必须使用相应的关联注释(@OneToOne@OneToMany@ManyToMany)进行声明。我建议您仔细阅读 Hibernate 入门指南 。请尝试以下操作以使您的代码示例正常工作

@Entity
public class Foo {
    @Id
    @GeneratedValue
    private Long id;

    @OneToOne
    private Bar bar;

    // getters and setters for id and bar
}

@Entity
public class Bar {
    @Id
    @GeneratedValue
    private Long id;

    private double x;

    // getters and setters for id and x
}

Each simple entity must be marked as an entity (with @Entity) and have an identifier (mostly a Long) as a primary key. Each non-primitive association/composition must be declared with the corresponding association annotation (@OneToOne, @OneToMany, @ManyToMany). I suggest you read through the Hibernate Getting Started Guide. Try the following to make your code example work

@Entity
public class Foo {
    @Id
    @GeneratedValue
    private Long id;

    @OneToOne
    private Bar bar;

    // getters and setters for id and bar
}

@Entity
public class Bar {
    @Id
    @GeneratedValue
    private Long id;

    private double x;

    // getters and setters for id and x
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文