Hibernate/JPA 多态和泛型类型

问题描述 投票:0回答:1

我尝试在 hibernate/JPA 实体中组合多态和泛型类型。以下是示例实体:

public class Cake {

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid2")
    @Size(max = 36)
    @Column(length = 36)
    @UserInterface(showInForm = false, showInList = false)
    @Comment("Primary key")
    private String id;

    @Version
    @NotNull
    @UserInterface(showInForm = false, showInList = false)
    @Comment("Version column for optimistic locking")
    private int version;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Ingredient {

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid2")
    @Size(max = 36)
    @Column(length = 36)
    @UserInterface(showInForm = false, showInList = false)
    @Comment("Primary key")
    private String id;

    @Version
    @NotNull
    @UserInterface(showInForm = false, showInList = false)
    @Comment("Version column for optimistic locking")
    private int version;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }
}
@Entity
public class Vegetable extends Ingredient {
}
@Entity
public class Fruit extends Ingredient {
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class CakeIngredientLink<T extends Ingredient> {

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid2")
    @Size(max = 36)
    @Column(length = 36)
    @UserInterface(showInForm = false, showInList = false)
    @Comment("Primary key")
    private String id;

    @Version
    @NotNull
    @UserInterface(showInForm = false, showInList = false)
    @Comment("Version column for optimistic locking")
    private int version;

    @ManyToOne(optional = false)
    private Cake cake;

    @ManyToOne(optional = false)
    private T ingredient;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public Cake getCake() {
        return cake;
    }

    public void setCake(Cake cake) {
        this.cake = cake;
    }

    public T getIngredient() {
        return ingredient;
    }

    public void setIngredient(T ingredient) {
        this.ingredient = ingredient;
    }
}
@Entity
public class CakeVegetable extends CakeIngredientLink<Vegetable> {
}
@Entity
public class CakeFruit extends CakeIngredientLink<Fruit> {
}

有了

@MappedSuperclass
,这个设计就成为可能。但是对于
@Inheritance(strategy = InheritanceType.JOINED)
我得到以下异常:

Property de.cakeproject.domain.CakeIngredientLink.ingredient has an unbound type and no explicit target entity. Resolve this Generic usage issue or set an explicit target attribute (eg @OneToMany(target=) or use an explicit @Type

我想使用多态查询来获取蛋糕的所有成分。这是

@MappedSuperclass
不可能实现的。有什么好的方法可以解决这个问题吗?

hibernate jpa generics polymorphism
1个回答
0
投票

我已经了解到并且也进行了测试,可以查询映射的超类。您唯一需要知道的是查询中需要实体的 FQN。

Hibernate还可以查询不是实体但由实体类扩展或实现的接口或基类。

拜尔东

我不确定这是否是 hibernate 特有的,但作为一个 hibernate 用户,这对我来说没问题。

© www.soinside.com 2019 - 2024. All rights reserved.