Hibernate映射(继承)
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用两个 < 来映射它
Person
上的 code>一对一 关联。作为旁注,如果您可以控制该架构,我建议您使用 继承映射 和 每个子类一个表,使用
type
列作为 person 表上的鉴别器。 这里是关于继承映射的教程。You could map that with two
One-To-One
associations onPerson
.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.