Hibernate映射(继承)

发布于 2024-12-05 21:36:25 字数 941 浏览 0 评论 0原文

我正在尝试在 Hibernate 中映射一些对象。这些对象之一是父亲,其他对象是孩子。换句话说,它们实现了继承。 父亲如下:

public class Person {

    private String id;
    private String name;
    private String surname;
    getters and setters ;
}

和孩子...

public class Employee {
      private BigDecimal salary;
      private String seccion;
      private Employee employee;
      private  Customer customer;

      getters and setters
}

public class Customer {
     private BigDecima CreditLimit;

     getter and setter
}

然后...我想将这些类映射到以下数据库模式中...

表 人 ID / NAME / SURNAME / ID_EMPLOYEE / ID_CUSTOMER

员工 ID_PERSON / SALARY / SECCION

客户 ID_PERSON / CREDIT_LIMIT

我的想法是每个角色可以是也可以不是客户/员工。换句话说,Customer 和 Employee 是 Person 的属性,但这些属性将存储在数据库中的独立表中。

为了获取角色的信用额度,我可以执行 persona.getCustomer().getCreditLimit();

始终控制此人是否是客户。

我希望你能帮助我,请原谅我的英语很差。我来自阿根廷。

提前致谢。

尼古拉斯

I'm trying to map some objects in Hibernate. One of these objects is father and the others are children. In other words they implement inheritance.
The father is as follow:

public class Person {

    private String id;
    private String name;
    private String surname;
    getters and setters ;
}

and children...

public class Employee {
      private BigDecimal salary;
      private String seccion;
      private Employee employee;
      private  Customer customer;

      getters and setters
}

public class Customer {
     private BigDecima CreditLimit;

     getter and setter
}

Then... I want to map these classes in the following database schema...

Table
Person
ID / NAME / SURNAME / ID_EMPLOYEE / ID_CUSTOMER

Employee
ID_PERSON / SALARY / SECCION

Customer
ID_PERSON / CREDIT_LIMIT

My idea is each persona can be or not a customer/employee. In other words Customer and Employee are properties of Person but these properties will be store in independents tables in the database.

For get the credit limit of a persona I can do persona.getCustomer().getCreditLimit();

Always making control if the Person is a Customer or is not.

I hope you can help me and excuse me my English is pretty poor. I'm from Argentina.

Thanks in advance.

Nicolas

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

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

发布评论

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

评论(2

岁月流歌 2024-12-12 21:36:25

您可以使用两个 < 来映射它Person 上的 code>一对一 关联。

作为旁注,如果您可以控制该架构,我建议您使用 继承映射每个子类一个表,使用 type 列作为 person 表上的鉴别器。 这里是关于继承映射的教程。

You could map that with two One-To-One associations on Person.

As a side note, if you've got control over that schema, I'd reccomend going for Inheritance Mapping and Table-per-subclass, using a type column as discriminator on the person table. Here is a tutorial on inheritance mapping.

感情洁癖 2024-12-12 21:36:25

您正在寻找的内容称为多态映射此处的示例 是你所需要的。

What you are looking for is known as Polymorphic Mapping, the example here is what you need.

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