Java 带有休眠注释的接口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2586583/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:32:56  来源:igfitidea点击:

Interfaces with hibernate annotations

javahibernatejpaannotationsgilead

提问by molleman

i am wondering how i would be able to annotate an interface

我想知道如何才能注释接口

@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 >();
}

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

上面和下面是 2 个实现名为 Hierarchy 的接口的类,文件夹类有一个层次结构列表,即文件夹或文件信息类

@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.

我在带有文件夹的层次结构列表中遇到映射异常,但我不知道如何正确映射类。

回答by Kris

A little Googling turned up...

一个小小的谷歌搜索出现了......

You can use interfaces internally but you can't map interfaces in hibernate, you have to map classes, regardless of whether you are using xml mapping or annotation mapping. hibernate is handling lifecycle of your persistent objects so it needs to know what class to instantiate so you need to provide this information to it... I am not even sure how what you suggesting would even look like? how would you provide implementation for given interface to hibernate at runtime to instantiate?

您可以在内部使用接口,但 不能在 hibernate 中映射接口,您必须映射类,无论您使用的是 xml 映射还是注释映射。hibernate 正在处理持久对象的生命周期,因此它需要知道要实例化哪个类,因此您需要向它提供此信息……我什至不确定您的建议会是什么样子?您将如何为给定的接口提供实现以在运行时休眠以实例化?

http://forum.springsource.org/showthread.php?t=67420

http://forum.springsource.org/showthread.php?t=67420

So it looks like you are out of luck.

所以看起来你运气不好。

回答by Péter T?r?k

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.

您可以在 Hibernate 中映射接口,作为继承层次结构的一部分。这对于 XML 映射来说肯定是可能的,正如Hibernate 参考资料 的第 9 章中所描述的那样。

Annotation based mapping is a different story though. I am not so familiar with it, but Java Persistence with Hibernateincludes examples for this too. Adapted to your case, it would look like

但基于注释的映射是另一回事。我对它不太熟悉,但是Java Persistence with Hibernate 也包含了这方面的示例。适应你的情况,它看起来像

@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:

但是,其他来源表明接口的注释支持可能还没有工作/稳定:

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)...

所以你可能需要进行试验,包括改变你的继承映射策略,也许把接口变成一个抽象类(如果可能的话——因为一个类只能扩展一个基类)......