-->
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.  [ 4 posts ] 
Author Message
 Post subject: Class Cast Exception with OneToMany association
PostPosted: Mon Jul 28, 2008 7:58 pm 
Newbie

Joined: Mon Jun 16, 2008 1:15 pm
Posts: 15
Hibernate version: the version comes with JBoss 4.2.2

I have two entities defined. First one like this:

Second entity:


My query is:

Name and version of the database you are using:
Oracle 10g
Full stack trace of any exception that occurs:


Last edited by peiguo on Sat Aug 02, 2008 5:54 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 8:22 am 
Regular
Regular

Joined: Wed Jan 11, 2006 12:49 pm
Posts: 64
Location: Campinas, Brazil
One problem is here:

Code:
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "item")
   @JoinColumn(name = "bitm_numb", referencedColumnName = "bitm_numb_chld")
   private List<Sos> sos;


First of all, you should not have mappedBy and @JoinColumn in the same relationship. Second and most importantly, property "item" in Sos is int, but Hibernate expects, as in any inverse relationship, that property "item" is of type DpacRelt.

Here is how your entities should look like:

Code:
@Entity
@Table(name = "dpac_relt", schema = "dcs3000")
public class DpacRelt
{
   @Id
   @Column(name = "bitm_numb_parn")
   private int parent;

   @Column(name = "bitm_numb_chld")
   private int child;

   @OneToMany(cascade = CascadeType.ALL, mappedBy = "item")
   private List<Sos> sos;

   public int getParent()
   {
      return parent;
   }

   public void setParent(int parent)
   {
      this.parent = parent;
   }

   public int getChild()
   {
      return child;
   }

   public void setChild(int child)
   {
      this.child = child;
   }

   public List<Sos> getSos()
   {
      return sos;
   }

   public void setSos(List<Sos> sos)
   {
      this.sos = sos;
   }
}

Code:
@Entity
@Table(name = "sos", schema = "dcs3000")
public class Sos
{
   @Id
   @Column(name = "sos_numb")
   private int sos;

   @ManyToOne
   @JoinColumn(name = "sos_item")
   private DpacRelt item;

   public int getSos()
   {
      return sos;
   }

   public void setSos(int sos)
   {
      this.sos = sos;
   }

   public DpacRelt getItem()
   {
      return item;
   }

   public void setItem(DpacRelt item)
   {
      this.item = item;
   }
}


Finally, I would recommend you to make your mapping closer to the requirements. I say that because I would expect "parent" and "child" to be some relationship instead of just ints. Also, an entity named Sos with a property of the same name seems odd.

Regards,

Henrique

_________________
Henrique Sousa
Don't forget to rate useful responses


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 3:35 pm 
Newbie

Joined: Mon Jun 16, 2008 1:15 pm
Posts: 15
Thanks for your reply. However what I want is unidirectional mapping, so the map on the other end is not required.

Your comment about mappedBy is certainly correct.



I think this is a Hibernate bug.


Last edited by peiguo on Sat Aug 02, 2008 5:53 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 7:00 pm 
Newbie

Joined: Mon Jun 16, 2008 1:15 pm
Posts: 15
"implements Serializable" appears to be a requirement of the spec. Although JPA does not enforce it, it chocks on it occasionally.


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

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.