简体   繁体   中英

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 .

We'll abstract out the common properties of the two classes into a FileSystemObject interface:

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 .

Now, assuming everything is set up correctly within pom.xml, when I try to generate some schema:

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. 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. (That is, you'd need something like FileSystemObject to be a common superclass they both inherit from, that defines ID at that level.)

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? This is why they need to share a common superclass if they are to be in the same collection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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