带有休眠注释的接口 [英] Interfaces with hibernate annotations
问题描述
我想知道如何注释界面
@Entity
@Table(name = "FOLDER_TABLE")
public class Folder implements Serializable, Hierarchy {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "folder_id", updatable = false, nullable = false)
private int fId;
@Column(name = "folder_name")
private String folderName;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "FOLDER_JOIN_FILE_INFORMATION_TABLE", joinColumns =
{ @JoinColumn(name = "folder_id") }, inverseJoinColumns =
{ @JoinColumn(name = "file_information_id") })
private List< Hierarchy > fileInformation = new ArrayList< Hierarchy >();
}
上面和下面是2个类,它们实现了一个名为Hierarchy的接口,文件夹类有一个Hierarchyies列表,是一个文件夹或一个文件信息类
above and below are 2 classes that implement an interface called Hierarchy, the folder class has a list of Hierarchyies being a folder or a fileinformation class
@Entity
@Table(name = "FILE_INFORMATION_TABLE")
public class FileInformation implements Serializable, Hierarchy {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "file_information_id", updatable = false, nullable = false)
private int ieId;
@Column (name = "location")
private String location;
}
我在网上搜索了一些注释或解决方法,但我无法映射简单的界面
I have searched the web for someway to annotate or a workaround but I cannot map the interface which is simply this
public interface Hierarchy {
}
我在带有文件夹的层次结构列表中遇到映射异常,但我不知道如何正确映射类.
I get a mapping exception on the List of hierarchies with a folder but I don't know how to map the class correctly.
推荐答案
您可以在 Hibernate 中映射接口,作为继承层次结构的一部分.这肯定可以通过 XML 映射实现,正如 Hibernate 参考资料的第 9 章,等等.
You can map interfaces in Hibernate, as part of inheritance hierarchies. This is quite surely possible with XML mapping, as it is described in Chapter 9 of the Hibernate reference, among others.
基于注释的映射是另一回事.我对它不太熟悉,但是 Java 持久化与 Hibernate 也包含了这方面的示例.适应你的情况,它看起来像
Annotation based mapping is a different story though. I am not so familiar with it, but Java Persistence with Hibernate includes examples for this too. Adapted to your case, it would look like
@MappedSuperclass
public interface Hierarchy {
}
@Entity
@Table(name = "FOLDER_TABLE")
public class Folder implements Serializable, Hierarchy { ... }
@Entity
@Table(name = "FILE_INFORMATION_TABLE")
public class FileInformation implements Serializable, Hierarchy { ... }
此映射将使用具有隐式多态性的每个具体类的表.
This mapping would use a table per concrete class with implicit polymorphism.
然而,其他来源表明接口的注解支持可能还没有工作/稳定:
However, other sources suggest that annotation support for interfaces may not be working / stable yet:
- 有一个关于使用
@Entity 的相关开放错误报告
带有接口的注释,其中还包含一些补丁, - 这是一个相关(相当老的)线程,描述了一种解决方法使用 XML 映射.
- there is a related open bug report on using the
@Entity
annotation with interfaces, which also includes some patches, - here is a related (fairly old) thread, describing a workaround using XML mapping.
所以你可能需要进行试验,包括改变你的继承映射策略,也许把接口变成一个抽象类(如果可能的话——因为一个类只能扩展一个基类)...
So you may need to experiment, including changing your inheritance mapping strategy, maybe turning the interface into an abstract class (if it's possible - since a class can only extend a single base class)...
这篇关于带有休眠注释的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!