-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: problem with "single table per inheritance hierarchy&qu
PostPosted: Thu Aug 10, 2006 11:22 am 
Newbie

Joined: Thu Aug 10, 2006 10:26 am
Posts: 1
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=?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.