java 使用 Hibernate 持久化接口集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2912988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Persist collection of interface using Hibernate
提问by Olvagor
I want to persist my litte zoo with Hibernate:
我想用 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
}
When I try to persist the zoo, Hibernate complains:
当我尝试坚持动物园时,Hibernate 抱怨:
Use of @OneToMany or @ManyToMany targeting an unmapped class: blubb.Zoo.animals[blubb.Animal]
I know about the targetEntity-property of @OneToManybut that would mean, only Dogs OR Cats can live in my zoo.
我知道targetEntity- 属性,@OneToMany但这意味着,只有狗或猫可以住在我的动物园里。
Is there any way to persist a collection of an interface, which has several implementations, with Hibernate?
有什么方法可以使用 Hibernate 持久化具有多个实现的接口集合?
采纳答案by Pascal Thivent
JPA annotations are not supported on interfaces. From Java Persistence with Hibernate(p.210):
接口不支持 JPA 注释。来自Java Persistence with Hibernate(p.210):
Note that the JPA specification doesn't support any mapping annotation on an interface! This will be resolved in a future version of the specification; when you read this book, it will probably be possible with Hibernate Annotations.
请注意,JPA 规范不支持接口上的任何映射注释!这将在规范的未来版本中解决;当您阅读本书时,可能会使用 Hibernate Annotations。
A possible solution would be to use an abstract Entity with a TABLE_PER_CLASSinheritance strategy (because you can't use a mapped superclass - which is not an entity - in associations). Something like this:
一个可能的解决方案是使用具有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>();
...
}
But there is not much advantages in keeping the interface IMO (and actually, I think persistent classes should be concrete).
但是保持接口 IMO 并没有太多优势(实际上,我认为持久类应该是具体的)。
References
参考
回答by yatskevich
I can guess that what you want is mapping of inheritance tree. @Inheritance annotation is the way to go. I don't know if it will work with interfaces, but it will definitely work with abstract classes.
我可以猜到你想要的是继承树的映射。@Inheritance 注释是要走的路。我不知道它是否适用于接口,但它肯定适用于抽象类。
回答by sammy
I think you have to annotate the interface too with @Entityand we have to annotate @Transienton all getters and setters of interface.
我认为您也必须对接口进行注释@Entity,我们必须对@Transient接口的所有 getter 和 setter进行注释。

