简体   繁体   English

是否可以保留列表 <E> 带有休眠注释,其中E是接口?

[英]Is it possible to persist a List<E> with hibernate annotations, where E is an interface?

This seems like it should be fairly straightforward, but I'm having trouble coming up with an elegant solution. 这似乎应该很简单,但是我很难提出一个优雅的解决方案。

Let's use the example of a basic file system, comprised of two hibernate-annotated classes, File and Folder . 让我们以一个基本文件系统为例,该系统由两个带有hibernate注释的类FileFolder组成

We'll abstract out the common properties of the two classes into a FileSystemObject interface: 我们将把这两个类的通用属性抽象到FileSystemObject接口中:

public interface FileSystemObject {
    public String getName();
    public URI getWhere();
}

And the two classes: 和两个类:

File : 档案

@Entity
@Table(name = "FILE")
public class File implements FileSystemObject, Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "FILE_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "NAME", nullable = false)
    @Index(name = "FILE_NAME", columnNames={"NAME"})
    private String name;

    @Column(nullable = false)
    private URI where;

    public File() {}

    public File(String name, URI where) {
        this.name = name;
        this.where = where;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public URI getWhere() {
        return where;
    }

    public void setWhere(URI where) {
        this.where = where;
    }
}

Folder : 资料夹

@Entity
@Table(name = "FOLDER")
public class Folder implements FileSystemObject, Serializable {

    private static final long serialVersionUID = 2L;

    @Id
    @Column(name = "FOLDER_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name = "NAME", nullable = false)
    @Index(name = "FOLDER_NAME", columnNames={"NAME"})
    private String name;

    @Column(nullable = false)
    private URI where;

    @CollectionOfElements
    @JoinTable(name = "FOLDER_CONTENTS",
               joinColumns = @JoinColumn(name = "FOLDER_ID"))
    private List<FileSystemObject> contents;

    public Folder() {}

    public Folder(String name, URI where, List<FileSystemObject> contents) {
        this.name = name;
        this.where = where;
        this.contents = contents;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public URI getWhere() {
        return where;
    }

    public void setWhere(URI where) {
        this.where = where;
    }

    public List<FileSystemObject> getContents() {
        return contents;
    }

    public void setContents(List<FileSystemObject> contents) {
        this.contents = contents;
    }
}

Logically, the contents of a Folder could be File or Folder , so it makes sense for the contents List to be of type FileSystemObject . 从逻辑上讲, Folder的内容可以是FileFolder ,因此目录List的类型为FileSystemObject是有意义的。

Now, assuming everything is set up correctly within pom.xml, when I try to generate some schema: 现在,假设当我尝试生成一些模式时,所有内容都在pom.xml中正确设置了:

mvn hibernate3:hbm2ddl

It throws the following error: 它引发以下错误:

Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:2.0:hbm2ddl (generate-ddl) on
project foo: Execution generate-ddl of goal org.codehaus.mojo:hibernate3-maven
plugin:2.0:hbm2ddl failed: Could not determine type for: com.foo.data.FileSystemObject, for
columns: [org.hibernate.mapping.Column(element)] -> [Help 1]

Hopefully someone can shed some light on this! 希望有人可以对此有所启发!

It is possible for the type there to be an Interface, but it will not work the way you've configured your entities. 该类型可能有一个Interface,但是它不适用于您配置实体的方式。 In order for both Entity types to be combined in the same collection, they need to share a common supertype that is itself an Entity. 为了将两种Entity类型组合在同一个集合中,它们需要共享一个本身就是Entity的公共超类型。 (That is, you'd need something like FileSystemObject to be a common superclass they both inherit from, that defines ID at that level.) (也就是说,您需要像FileSystemObject这样的东西作为它们都继承的通用超类,该超类在该级别定义ID。)

The problem is, consider this query: 问题是,请考虑以下查询:

select c from Folder f, f.contents c where f.name = 'FOLDER' and c.id = 3;

If there are both a Folder and a File that have id 3, how is it supposed to know what you want? 如果同时存在ID为3的文件夹和文件,应该如何知道您想要什么? This is why they need to share a common superclass if they are to be in the same collection. 这就是为什么如果它们要属于同一集合,则需要共享一个公共超类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM