简体   繁体   English

休眠中同一表上的一对多映射

[英]One to many mapping on same table in hibernate

I'm not good in hibernate. 我冬眠不好。 I have a bad database design in which things are put together into one table. 我的数据库设计不好,无法将所有内容放到一张表中。 I want do one to many mapping on the same table. 我想在同一张表上进行一对多映射。

My schema is as follows. 我的架构如下。

---------------------------------------------------------------------------------------
svc_id | svc_status| contract_no| contract_detail1| contract_detail2| contract_detail3
--------------------------------------------------------------------------------------
svc100 | enabled   | a1         | a1-pro1         | a1-pro2         | a1-pro3             
svc100 | enabled   | b1         | b1-pro1         | b1-pro2         | b1-pro3             
svc100 | enabled   | c1         | d1-pro1         | c1-pro2         | d1-pro3             
svc100 | enabled   | d1         | d1-pro1         | d1-pro2         | d1-pro3             
svc400 | disabled  | y1         | yyy-pro1        | yyy-pro2        | yyy-pro3           
svc400 | disabled  | z1         | z1-pro1         | z1-pro2         | z1-pro3      

The svc_status is always unique per svc_id. 每个svc_id的svc_status始终是唯一的。 I want to have an 1-to-many hibernate mapping in which I can store the values into following (or similar) java bean. 我想有一个一对多的休眠映射,在其中可以将值存储到以下(或类似的)Java Bean中。

 Class Service {
     serviceId; //svc100
     serviceStatus; //enabled
     Set<Contract> contracts; //list of all 'svc100' contracts
 }

 Class Contract {
     contract_no;
     contract_detail1;
     contract_detail2;
     contract_detail3;
 }

Please help. 请帮忙。

Since you have a single table, having more than one mapped entity class doesn't make sense. 由于您只有一个表,因此拥有多个映射的实体类没有任何意义。 You have two option here: 您有两种选择:

  1. Re-design your database schema in order to have two tables: service(svc_id, svc_status) and contract(contract_no, contract_detail1, contract_detail2, contract_detail3) . 重新设计数据库架构,使其具有两个表: service(svc_id, svc_status)contract(contract_no, contract_detail1, contract_detail2, contract_detail3)

  2. If 1. is not possible (I'm not sure which are the considerations for which you're tied to the poor design), you should create Service instances at the application level, based on the set of Contract instances (without making Service a mapped entity!!). 如果不可能1.(我不确定与不良设计相关的考虑因素是什么),则应基于Contract实例集在应用程序级别创建Service实例(不要使Service a映射的实体!)。

One-to-many relationships on the same table are allowed (so-called self-referencing tables) and can be easily mapped, but I don't see how they can serve your purpose. 允许在同一表上建立一对多关系(所谓的自引用表),并且可以轻松地进行映射,但是我看不到它们如何满足您的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM