• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate ClassCastException

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am newbie with Hibernate and I am doing pretty much everything by the book

I have this class

public class AbstrFile{

... other fields ...
HashSet files = new HashSet();

... methods ....
}

This is a kind of representation of File in file system and every
file can have many children (placed in "files" field).

But when I try to do

session.save(AbstrFile f) // of course this is just pseudo code I got
this:

any ideas???


 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may not be your exact problem, but try to avoid using concrete collections in your method signatures. Hibernate will end up replacing them under the covers with its own collections for dealing with things like lazy loading. So instead of

private HashSet files = new HashSet();
public HashSet getFiles();
public void setFiles(HashSet set);

change it to

private Set files = new HashSet();
public Set getFiles();
public void setFiles(Set set);

Note that you can still start out with a HashSet as the default (for a transient object) but you can't count on it once Hibernate starts managing the Object.
 
asirob civokviz
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Patrick for your quick response but unfortunately that is not the problem because, as I already wrote, I do everything by the book (tutorial) so I did use Set instead of HashSet

If I understood correctly I can do following (has to do with AbstrFile class previously explained:

how AbstrFile is created is not important and assume
that session is an instance of Hibernate session


and all AbstrFile instances will be saved to database???
There is no need to save each one but it is sufficient to save parent???
This is how I do this in my code. And I did cascade all operations.

thanks again
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CAn you please paste yur XMLs and your classes code too?
 
asirob civokviz
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok,
here is a mapping for JCatFile (problematic class and mapping):



Here is corresponding java file




and here is when it reports exception (excerpt):



thanks in advance...
 
asirob civokviz
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found it!!!

problem was here


sort="natural" caused ClassCastException... I guess that is because I used HashSet and it can not be sorted?!!

Now it works...

thanks everyone for their effort
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
I am also a newbie for Hibernate. And I wrote my codes with using ArrayLists. For example

private ArrayList<RegionFuelPrice> fuelPrices;

is declared in XML file like;

<list name="fuelPrices" cascade="all, delete-orphan">
<key column="id"/>
<index column="index_id"/>
<one-to-many class="RegionFuelPrice"/>
</list>


But everytime, i got ClassCastException like my friend. What do you think? When i look at the tutorials i can not find a reason for this code for not working. Can you help please?
 
reply
    Bookmark Topic Watch Topic
  • New Topic