1

考虑以下实体。我打算让 Child 类引用两个派生食品类( Local Food 或 Foreign )中的任何一个。这是一个人为的示例,我的真实域对象非常复杂,因此组合和使用例如 FoodType 列不是一个选项,因为两个 Food 子类仅在少数特征上相似。

@MappedSuperclass
public abstract class Food {

}



@Entity
public class LocalFood extends Food {

private long id;
private String name;
}


@Entity
public class ForeignFood extends Food {

    private long id;
    private String name;
}



@Entity
public class Child {    
private Food food; //Base Class needed here 
@ManyToOne()
public Food getFood() {
    return food;
}
}

Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on                     com.sample.Child.food references an unknown entity: com.sample.Food

也不使用继承和鉴别器。

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Food {

private long id; // set , get (Auto gen) 
}

是否有可能使这种映射起作用?

4

1 回答 1

2

JB Nizet 是对的。现在食物课看起来像这样。

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

 private long id;

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

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 public long getId() {
    return id;
 }

}

并且从子类中删除了 id。

于 2012-07-18T14:04:00.653 回答