0

2 つのエンティティがあり、1 つはアイテム、もう 1 つはフルーツです。最初に他のスーパークラスとして実装したいのですが、スーパークラスのすべてのパラメーターをそのサブクラスに継承させたいと思っています。

また、データベースを構築してそのスーパークラス テーブルとそのすべてのプロパティをサブクラス テーブルにバインドする場合、つまり、さらにサブクラスを作成する場合、すべてのサブクラスがスーパー クラスにあるはずの値を書き込む必要はありません。

どうすればこれを達成できますか?

item.java

  @MappedSuperclass
  @DiscriminatorColumn(name="Item")
  @Inheritance(strategy = InheritanceType.JOINED)
  public abstract class Item implements java.io.Serializable
   {
      // here goes all values of the class Item
   }

果物.java

   @Entity
   @PrimaryKeyJoinColumn(name="ART_ID")
   public class Fruit extends Item implements java.io.Serializable{
      // here goes proper values of this class 
   }

データベース.db

   create table Item
   (
    ITEM_ID          bigint not null auto_increment,
    ITEM_NAME         varchar(25) not null,
    );

   create table Fruit
    (
    FRUIT_DATEFROZEN date,
    );

);
4

1 に答える 1

1

私が持っている複雑な継承ツリーに対して、次のようなものを実装しました。

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Item
{
    @Id
    Long id;

    String name;
}

@DiscriminatorValue("FRUIT")
@SecondaryTable( name = "fruit" )
public class NumberAnswer extends Item
{
    @Column(table = "fruit")
    Date frozen;
}

create table Item
(
  ID     bigint not null auto_increment,
  NAME   varchar(25) not null,
);

create table Fruit
(
  ID     bigint not null
  FROZEN date,
);
于 2012-08-29T22:18:39.867 に答える