与Hibernate注解接口 [英] Interfaces with hibernate annotations

查看:171
本文介绍了与Hibernate注解接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我怎么会可以注解的接口

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

上面和下面是实现所谓的层次接口2类,文件夹类有Hierarchyies是一个文件夹或fileinformation类

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;

我已经serached网上寻找某种方式来注释或解决方法,但我不能映射这简直就是在这个界面

i have serached the web for someway to annotate or a workaround but i cannot map the interface which is simply this

public interface Hierarchy {

 }

我得到一个文件夹hierarchyies的目录映射exeception,但我不知道如何
正确映射类

i get a mapping exeception on the List of hierarchyies with a folder but i dont 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:

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

这篇关于与Hibernate注解接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆