Hi
We have a model with inheritance that looks like this (simplified):
Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class PathElement extends CoreBase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parentPathElementId")
private PathElement parentPathElement;
@OneToMany(mappedBy = "parentPathElement", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
private List<PathElement> subPathElements = new ArrayList<PathElement>();
...
}
@Entity
@PrimaryKeyJoinColumn(name = "pathElementId")
public class Subsection extends PathElement {
...
}
@Entity
@PrimaryKeyJoinColumn(name = "pathElementId")
public class Section extends PathElement {
int someProp;
...
}
In my database I have the following data:
Code:
PATHELEMENT
id parentPathElementId
1 null
2 1
3 1
SECTION
pathElementId someProp
1 33
SUBSECTION
pathElementId
2
3
Now I try to make a query to this data:
Code:
Subsection subsection = (Subsection) _sess.createCriteria(SubSection.class).
.add(Expression.eq("id", 2)
.createCriteria("parentPathElement")
.uniqueResult();
Now, my problem is that subsection.parentPathElement is a CGLIB enhanced class that extends PathElement, not Section. This means, for example, that I cannot cast parentPathElement to Section and access its properties.
Is there an elegant possiblity to get the correct type from hibernate? I'm using Hiberate 3.2 final.
Thanks for your help
Patric