-->
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: Inheritance and ManyToOne link
PostPosted: Mon Oct 23, 2006 4:47 am 
Newbie

Joined: Mon Oct 23, 2006 4:29 am
Posts: 1
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


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.