Olv*_*gor 29 java hibernate interface
我想用Hibernate坚持我的小动物园:
@Entity
@Table(name = "zoo")
public class Zoo {
@OneToMany
private Set<Animal> animals = new HashSet<Animal>();
}
// Just a marker interface
public interface Animal {
}
@Entity
@Table(name = "dog")
public class Dog implements Animal {
// ID and other properties
}
@Entity
@Table(name = "cat")
public class Cat implements Animal {
// ID and other properties
}
Run Code Online (Sandbox Code Playgroud)
当我试图坚持动物园时,Hibernate抱怨道:
Use of @OneToMany or @ManyToMany targeting an unmapped class: blubb.Zoo.animals[blubb.Animal]
Run Code Online (Sandbox Code Playgroud)
我知道targetEntity- @OneToMany但这意味着,只有狗或猫可以住在我的动物园.
有没有办法用Hibernate来持久化一个具有多个实现的接口集合?
Pas*_*ent 27
接口不支持JPA注释.从Java Persistence with Hibernate(p.210):
请注意,JPA规范不支持接口上的任何映射注释!这将在规范的未来版本中得到解决; 当您阅读本书时,可能会使用Hibernate Annotations.
一种可能的解决方案是使用带有TABLE_PER_CLASS继承策略的抽象实体(因为您不能在关联中使用映射的超类 - 它不是实体).像这样的东西:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractAnimal {
@Id @GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
...
}
@Entity
public class Lion extends AbstractAnimal implements Animal {
...
}
@Entity
public class Tiger extends AbstractAnimal implements Animal {
...
}
@Entity
public class Zoo {
@Id @GeneratedValue
private Long id;
@OneToMany(targetEntity = AbstractAnimal.class)
private Set<Animal> animals = new HashSet<Animal>();
...
}
Run Code Online (Sandbox Code Playgroud)
但是保持接口IMO没有太大的优势(实际上,我认为持久化类应该是具体的).
| 归档时间: |
|
| 查看次数: |
15752 次 |
| 最近记录: |