是否可以使用hibernate注释持久保存List <e>,其中E是接口?</e>

时间:2012-10-10 01:24:50

标签: java hibernate list interface annotations

这似乎应该相当简单,但我在提出一个优雅的解决方案时遇到了麻烦。

让我们使用基本文件系统的示例,该系统由两个带有hibernate注释的类组成,文件文件夹

我们将这两个类的公共属性抽象为 FileSystemObject 接口:

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

这两个班级:

文件

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

文件夹

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

逻辑上,文件夹的内容可以是文件文件夹,因此内容列表的类型为 FileSystemObject的

现在,假设在pom.xml中正确设置了所有内容,当我尝试生成一些模式时:

mvn hibernate3:hbm2ddl

它会抛出以下错误:

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]

希望有人可以对此有所了解!

1 个答案:

答案 0 :(得分:1)

那里的类型可能是一个接口,但它不会像你配置实体那样工作。为了将两个实体类型组合在同一个集合中,它们需要共享一个本身就是实体的公共超类型。 (也就是说,你需要像FileSystemObject这样的东西,它们都是继承的公共超类,它定义了那个级别的ID。)

问题是,请考虑以下问题:

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

如果同时存在ID为3的文件夹和文件,那该怎么知道你想要什么?这就是为什么他们需要共享一个共同的超类,如果他们要在同一个集合中。