Hi,
I am using the inheritance strategy "single table per class hierarchy" to implement the directory structure of a file system (see example below). Everything works fine except the fetching of the collections: when fetching the subdirectories with dir.getSubdirs(), I also get the files; further, when fetching the files with dir.getFiles() I get an exception because contained subdirectories are also fetched. Looking at the executed SQL statements it is clear why - hibernate does not recognize the element type of the "subdirs" and "files" set. Specifying the "targetEntity" seems not to change anything. I also implemented the mapping with XML mapping files but got the same results.
Sure, a trivial solution to the problem is to manually implement the queries in the getSubdirs() and getFiles() methods. But I think there should be a better solution for this.
Any help and feedback is appreciated.
Thanks,
--Martin.
Hibernate version:
hibernate-3.2.0.cr2
hibernate-annotations-3.2.0.CR1
Mapping documents:
Code:
@Entity
@Table(name="entity")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="entity_type",
discriminatorType=DiscriminatorType.STRING
)
public abstract class FamixEntity {
@Id @GeneratedValue
private Long id;
private String uniqueName;
public FamixEntity() {
}
public FamixEntity(String name) {
this.uniqueName = name;
}
protected void setId(Long id) {
this.id = id;
}
@Id
public Long getId() {
return id;
}
public String getUniqueName() {
return uniqueName;
}
public void setUniqueName(String uniqueName) {
this.uniqueName = uniqueName;
}
}
@Entity
@DiscriminatorValue("file")
public class File extends FamixEntity {
public File() {
super();
}
public File(String name) {
super(name);
}
}
@Entity
@DiscriminatorValue("directory")
public class Directory extends FamixEntity {
private Set<Directory> subdirs = new HashSet<Directory>();
private Set<File> files = new HashSet<File>();
public Directory() {
super();
}
public Directory(String name) {
super(name);
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, targetEntity=seal.famix.model.File.class)
@JoinColumn(name="parent_id")
public Set<File> getFiles() {
return files;
}
public void setFiles(Set<File> files) {
this.files = files;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, targetEntity=seal.famix.model.Directory.class)
@JoinColumn(name="parent_id")
public Set<Directory> getSubdirs() {
return subdirs;
}
public void setSubdirs(Set<Directory> subdirs) {
this.subdirs = subdirs;
}
}
Name and version of the database you are using:
psql (PostgreSQL) 8.1.3 running on linux
The generated SQL (show_sql=true):
select
subdirs0_.parent_id as parent4_1_,
subdirs0_.id as id1_,
subdirs0_.id as id0_0_,
subdirs0_.uniqueName as uniqueName0_0_
from
Entity subdirs0_
where
subdirs0_.parent_id=?