【问题标题】:How to use generic type parameter with Hibernate 5?如何在 Hibernate 5 中使用泛型类型参数?
【发布时间】:2017-10-22 14:50:18
【问题描述】:

下面是类定义

界面

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

}

我正在查看的生成的类如下所示。

我的班级表

-------------------------
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(文档)?

【问题讨论】:

    标签: java hibernate spring-data-jpa hibernate-ogm hibernate-5


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多