繁体   English   中英

如何在Hibernate 5中使用泛型类型参数?

[英]How to use generic type parameter with Hibernate 5?

以下是类定义

接口

public interface myInterface{

    void myMethod1();

}

实现上述接口的MyClass1类

 @Entity
 public class MyClass1 implements MyInterface{

    @Id
    private int id;
    private String field1;
    private String field2;

    // Getter and setter

}

实现上述接口的MyClass2类

 @Entity
 public class MyClass2 implements MyInterface{

    @Id
    private int id;
    private String field3;
    private String field4;

    // Getter and setter

}

最后是具有类型参数列表的实体类。

@Entity
public class MyClass{

    @Id
    priviate int id;
    private String field5;
    private String field6;
    private List<? extends MyInterface> mylist;

    //getter and setter

}

我正在看的生成的类看起来像下面的东西。

MyClass表

-------------------------
id | field5 | field6
-------------------------
1  | abc    | def
2  | abc    | def
3  | abc    | def

MyClass1表

-------------------------
id | field1 | field2 | myclass_fk
-------------------------
1  | abc    | def    | 2
2  | abc    | def    | 2
3  | abc    | def    | 1

MyClass2表

-------------------------
id | field3 | field4 | myclass_fk
-------------------------
1  | abc    | def    | 3
2  | abc    | def    | 1
3  | abc    | def    | 1

我尝试在列表上使用@OneToMany ,其中targetType为MyInterface但它失败了,错误不是实体类。

编辑

是否可以使用Hibernate OGM实现,最好使用基于图形或Mongo(文档)?

问题是Hibernate在加载MyClass时不知道从哪个表加载“mylist”。 请查看https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/chapters/domain/inheritance.html

我可以使用@ManyToAny注释实现这一点 ,但我需要修改类位。

暂无
暂无

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

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