hibernate如何存储可嵌入的obj
有点愚蠢的问题,但我没有在任何地方找到答案。 @Embeddable 对象如何存储在数据库中,是一种与 FK 的 OneToOne 还是...例如,如果我有:
@Entity
public class User {
private Long id;
@Id
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
private Address address;
@Embedded
public Address getAddress() { return address; }
public void setAddress() { this.address = address; }
}
@Embeddable
public class Address {
private String street1;
public String getStreet1() { return street1; }
public void setStreet1() { this.street1 = street1; }
}
表应该是什么样子?
kind of stupid question but i didn't find the answer anywhere. How @Embeddable object is stored in the database, is it kind of OneToOne with FK or... For example if i have:
@Entity
public class User {
private Long id;
@Id
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
private Address address;
@Embedded
public Address getAddress() { return address; }
public void setAddress() { this.address = address; }
}
@Embeddable
public class Address {
private String street1;
public String getStreet1() { return street1; }
public void setStreet1() { this.street1 = street1; }
}
How the table(s) should look like?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嵌入对象的字段作为列添加到所属实体的表中。
因此,您将拥有一个包含字段
id
和street1
的表User
。The embedded object's fields are added as columns to the table of the owning Entity.
So you will have a table
User
with fieldsid
andstreet1
.这非常简单:嵌入对象中的所有列都将合并到父对象的列中,从而形成一个表。在您的示例中,您最终将得到一个表
User
,其中包含列:id
和street1
。因此,嵌入对象基本上是一种对一个表中的列进行逻辑分组的方法。It's very simple: all columns from the embedded object will be merged into columns of the parent, resulting with a single table. In your example you will end up with a table
User
containing columns:id
andstreet1
. So embedded objects are basically a way to logically group columns within one table.