如何使用休眠将一些实体的值存储在另一个表中?

发布于 2024-08-27 11:20:49 字数 802 浏览 7 评论 0原文

有没有一种简单的方法可以使用 hibernate 将某些字段保留在另一个类和表中。

例如,我有一个包含 namesurnameemailaddress1Person 类>、地址2城市国家字段。我希望我的课程是:

public class Person
{
   private String name;
   private String surname;
   private String email;
   private Address address;
   // ..
}

public class Address
{
   private Person person; // to whom this belongs
   private String address1;
   private String address2;
   private String city;
   private String country;
   // ..
}

我想将地址存储在另一个表中。实现这一目标的最佳方法是什么?

编辑:我正在使用注释。它不一定是我所描述的方式,我正在寻找最佳实践。

编辑 2: 地址的 ID 是什么?

附言。如果有一种方法可以使 Address 不可变(用作值对象)那就更好了,或者可能不是,因为我从错误的角度思考一切:)

is there a simple way to persist some of the fields in another class and table using hibernate.

For example, I have a Person class with name, surname, email, address1, address2, city, country fields. I want my classes to be:

public class Person
{
   private String name;
   private String surname;
   private String email;
   private Address address;
   // ..
}

public class Address
{
   private Person person; // to whom this belongs
   private String address1;
   private String address2;
   private String city;
   private String country;
   // ..
}

and I want to store Address in another table. What is the best way to achieve this?

Edit: I am using annotations. It does not have to be the way I described, I am looking for best practices.

Edit 2: What will be the Id of Address?

PS. If there is a way to make Address immutable (to use as a value object) that is even better, or maybe not because I thought everything from wrong perspective :)

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

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

发布评论

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

评论(2

哆兒滾 2024-09-03 11:20:49
  1. Address 映射为实体并添加主键(自动生成的 ID)
  2. PersonAddress 之间的关系映射为 one -to-one(每个字段上的@OneToOne
  1. map Address as an entity and add a primary key (an auto-generated id)
  2. map the relation between Person and Address as one-to-one (@OneToOne on each field)
或十年 2024-09-03 11:20:49

使用 Hibernate 3.5 可以定义外部生成器(又名 JPA 映射),详细信息为 此处

这是非常简单的 Person 应该实现 Serialized 然后将 @Id 注释添加到 person 中。

@Entity
@AccessType(value = "field")
@Table(name = "addresses")
public class Address
{
   @Id
   @OneToOne
   @JoinColumn(name = "person_id")
   private Person person;
   // ...
}

还有一个替代方案,但我真的很喜欢第一个:

@Entity
@AccessType(value = "field")
@Table(name = "addresses")
public class Address
{
   @Id
   private int personId;

   @MapsId
   @OneToOne
   @JoinColumn(name = "person_id")
   private Person person;
   // ...
}

With Hibernate 3.5 it is possible to define foreign generators (aka. JPA mapping), details are here.

It is pretty straight forward Person should implement Serializable then @Id annotation is added to person.

@Entity
@AccessType(value = "field")
@Table(name = "addresses")
public class Address
{
   @Id
   @OneToOne
   @JoinColumn(name = "person_id")
   private Person person;
   // ...
}

There is an alternative but I really like the first one:

@Entity
@AccessType(value = "field")
@Table(name = "addresses")
public class Address
{
   @Id
   private int personId;

   @MapsId
   @OneToOne
   @JoinColumn(name = "person_id")
   private Person person;
   // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文