-->
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.  [ 5 posts ] 
Author Message
 Post subject: OneToMany ClassCastException configuration problem
PostPosted: Sat Jun 14, 2008 10:39 am 
Newbie

Joined: Fri Jan 18, 2008 1:59 pm
Posts: 10
Hibernate version: 3.2
Name and version of the database you are using: MySQL 5

I'm trying to setup a one to many relationship, but only get one part of the "many" class instead of the entire object. Is this possible? Here's my code. If you have any ideas on how to do this, I'd really appreciate any help you can offer.

Code:
@Entity
@Table(name = "locations")
public class Location {
...
   @OneToMany(targetEntity = Type.class)
   @JoinTable(
      name = "locations_types",
      joinColumns = @JoinColumn(name = "location_id"),
      inverseJoinColumns = @JoinColumn(name = "type_id")
   )
   @OrderBy(value = "type")
   private List<String>      types;
...
}

@Entity
@Table(name = "types")
public class Type {

   @Id
   @ManyToOne(targetEntity=Location.class)
   private int      id;

   @Column
   private String   type;
...
}


Full stack trace of any exception that occurs:
java.lang.ClassCastException: com.package.path.model.Type

Thanks,
Eric


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 11:03 am 
Newbie

Joined: Fri Jan 18, 2008 1:59 pm
Posts: 10
After reading the documentation more, I realized that I should be using @CollectionOfElements instead. Like so:

Code:
@Entity
@Table(name = "locations")
public class Location {
...
   @CollectionOfElements
   @JoinTable(
      name = "types_with_location",
      joinColumns = @JoinColumn(name = "location_id")
   )
   @Column(name = "type", nullable = false)
   @IndexColumn(name = "type_id")
   private List<String>      types;
...
}


Where the "types_with_location" view is:
Code:
select
   lt.location_id,
   lt.type_id,
   t.type
from
   types t join
   locations_types lt on ( t.id = lt.type_id )


However, my types list is filled with a bunch of null values, in addition to the correct string values. Trying to figure that one out now.

Thanks,
Eric


Top
 Profile  
 
 Post subject: Re: OneToMany ClassCastException configuration problem
PostPosted: Sat Jun 14, 2008 12:25 pm 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
What you are doing seems very curious and odd to me!

ebessette wrote:
...
@OneToMany(targetEntity = Type.class)
@JoinTable(
name = "locations_types",
joinColumns = @JoinColumn(name = "location_id"),
inverseJoinColumns = @JoinColumn(name = "type_id")
)
@OrderBy(value = "type")
private List<String> types;

@Entity
@Table(name = "types")
public class Type {

@Id
@ManyToOne(targetEntity=Location.class)
private int id;
...
}
[/code]



First, you need not "targetEntity=" - if you would map to a
Code:
private List<Type> types;
,

and on the reverse side to

Code:
private Location loc;

- not to an id (the id is in the database, but not in Java code.

Remember: Hibernate and JPA are working on POJOs; so you just have to code bidirectional relations using Java objects on both sides!

Or, did I misunderstand your effort completely?

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 12:40 am 
Newbie

Joined: Fri Jan 18, 2008 1:59 pm
Posts: 10
I actually got it to work by removing all the nullable=false references and the @IndexColumn annotation. I'm really not sure why it works, but it does, so I'm happy. :)

Thanks,
Eric


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 10:56 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, thanks for the update. Maybe if you figure out why it works like that, you'll give an update as well. :)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.