English 中文(简体)
利用Hibernate收集接口
原标题:Persist collection of interface using 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
}

当我试图维持动物园时,Hibernate抱怨:

Use of @OneToMany or @ManyToMany targeting an unmapped class: blubb.Zoo.animals[blubb.Animal]

我知道<代码>targetEntity-property of @OneToMany,但这意味着只有Dogs OR 煤炭可以生活在我的动物群中。

是否有办法与解放国一道继续收集一个接口,该接口有几个实施?

最佳回答

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.

可能的解决办法是使用一个具有<代码>的抽象实体。 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>();

    ...
}

但是,在保持海事组织的接口方面并没有多大优势(实际上,我认为,持续班级应当是具体的)。

References

问题回答

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.

我认为,你也必须把接口同<条码>@ Entity联系起来,我们必须向所有接线器和界面标出<条码>@Transient<>。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签