[英]Hibernate: mapping many-to-one association to different parent table depending on type?
I have tables which look like this: 我有看起来像这样的表:
Item table: 项目表:
ItemName Varchar2(200)
ItemId Number
OwnerId Number
OwnerType number
Person table: 人员表:
PersonName Varchar2(200)
PersonId Number
Company table: 公司表:
CompanyName Varchar2(200)
CompanyId Number
When the ownerType for an Item is '1', the ownerId of the item is a foreign key PersonId of the 'person' table. 当某项的ownerType为'1'时,该项的ownerId是'person'表的外键PersonId。 When the OwnerType is '2', the ownerId is a foreign key of the CompanyId of the Company table. 当OwnerType为'2'时,ownerId是Company表的CompanyId的外键。
I didn't do the data modelling & I don't have the option of changing the schema. 我没有进行数据建模,也没有选择更改架构的选项。
I'd like to solve two problems: (1) I'd like a mapping file for the Item table which would give me the items's owner. 我想解决两个问题:(1)我想要一个Item表的映射文件,该文件将给我项目的所有者。 (2) I'd like mapping files for the Person & Company tables which would give me the Items owned by the Person or Company, respectively. (2)我想要“个人与公司”表的映射文件,这将分别给我“个人”或“公司”拥有的项目。
Note: a CompanyId and a PersonId can be the same number, so an item is only owned by a Person when person.personid = item.ownerid and item.ownertype = 1. 注意:CompanyId和PersonId可以是相同的数字,因此,只有当person.personid = item.ownerid和item.ownertype = 1时,项才归Person所有。
I can control the mapping files & the object diagram, so I could create an Owner interface or super-class. 我可以控制映射文件和对象图,因此可以创建Owner接口或超类。
Can Hibernate do this sort of thing? Hibernate可以做这种事情吗?
Case 1
: you can use the concept of Table per class hierarchy
. Case 1
:您可以使用Table per class hierarchy
的Table per class hierarchy
的概念。 Case 2
: map items as set for person/company class with where condition on the set mapping. Case 2
:为人员/公司类设置的映射项,其中条件在设置的映射上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.