【问题标题】:JPA Hibernate inheritanceJPA休眠继承
【发布时间】:2018-01-07 03:40:24
【问题描述】:

假设我有一个“游戏”的抽象超类

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class BaseGame {

    @OneToMany(mappedBy="baseGame")
    public List<CardList> decks;

    //... lots of other fields
}

卡片列表在哪里:

@Entity
public class CardList{

    @ManyToOne
    @JoinColumn(name="basegame_id", nullable=false)
    @JsonIgnore
    private BaseGame baseGame;

    @ManyToMany
    public List<BaseCard> cards;   

    //... lots of other fields
}

问题出现在多对多关系中。 假设我有 5 个游戏,每年可能会手动添加一两个游戏,它们都有不同的字段 + 信息。

游戏 A 有一个 cardA extends BaseCard 的列表

@Entity
public class GameA extends BaseGame{

}

//somehow have the cardList in the BaseGame super class of this object
// actually use the cardA instead of just "BaseCard"

@Entity
public class CardA extends BaseCard{

}

游戏 B 有一个 cardB extends BaseCard 的列表

@Entity
public class GameB extends BaseGame{

}

//somehow have the cardList in the BaseGame super class of this object
// actually use the cardB instead of just "BaseCard"

@Entity
public class CardB extends BaseCard{

}

我的问题是,因为我真的很想用一个超类“BaseCard”巧妙地处理这个问题(因为它们也有很多共同的领域) 我是否需要为每个游戏创建多个 CardList 类并将它们从超类移动到子类?

我真正想知道的是:我可以根据标准制作或告诉 Hibernate/JpA“使用”BaseCard 的不同子类吗?

在这种情况下,标准将是“BaseGame 的子类”

【问题讨论】:

    标签: java hibernate jpa inheritance


    【解决方案1】:

    简而言之,不,你不能。

    每个基类都需要一个具体 Card 类的 List,但您仍然可以使用继承,并且您的抽象 BaseGame 类应该有一个返回卡片的抽象方法 getCards()。

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 1970-01-01
      • 2011-04-03
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      相关资源
      最近更新 更多