简体   繁体   English

使用泛型将多对多休眠

[英]Hibernate Many to one bidirectional using generics

I have an entity class which I need to use at multiple places. 我有一个实体类,需要在多个地方使用。 So I created a class with generics as below 所以我用下面的泛型创建了一个类

class Output<T> {
    ...
    private String referencedEntityName; 
    private T referencedEntity; 
    ...

}

I've two other classes which are using this class 我还有另外两个正在使用该课程的课程

class Process {
     ...
     private List<Output<Process>> outputs;
     ...
}

Another class which uses Output<T> is 使用Output<T>另一个类是

class Machine {
    ...
    private List<Output<Machine>> outputs; 
    ...

}

When stored in database it should look like this 当存储在数据库中时,它应该看起来像这样

+----+------------------------+----------------------+
| id | referenced_entity_name | referenced_entity_id |
+----+------------------------+----------------------+
| 1  | Process                | 123                  |
+----+------------------------+----------------------+
| 2  | Process                | 234                  |
+----+------------------------+----------------------+
| 3  | Machine                | 345                  |
+----+------------------------+----------------------+

How do I achieve this? 我该如何实现?

PS This is not exactly an inheritance problem like this . PS这不是像这样的继承问题。 Though it could be solved using an interface solution like DiscriminatorValue but I'm not sure. 尽管可以使用DiscriminatorValue类的接口解决方案来解决,但我不确定。

Here, there is a relation between two entities, one entity is referred in a few other entities. 在此,两个实体之间存在关系,一个实体在其他几个实体中被引用。 I think that should be clear by the use of generics. 我认为使用泛型应该很清楚。 Also, this solution could be designed in some other way by maintaining the same relations. 同样,可以通过保持相同关系以其他方式设计此解决方案。 I'm open to that as well. 我也对此持开放态度。

Read about inheritance in JPA. 了解有关JPA中的继承的信息。 With inheritance strategy SINGLE_TABLE you will get exactly table like you posted . 使用继承策略SINGLE_TABLE,您将获得与发布完全相同的表 See https://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Example_single_table_inheritance_table_in_database 参见https://en.wikibooks.org/wiki/Java_Persistence/Inheritance#Example_single_table_inheritance_table_in_database

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

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