开发者

Hibernate mapping (inheritance)

开发者 https://www.devze.com 2023-04-06 05:55 出处:网络
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.

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 c开发者_JS百科lass 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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号