-->
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.  [ 6 posts ] 
Author Message
 Post subject: CastException from Database to a HashSet
PostPosted: Fri May 21, 2010 12:36 pm 
Newbie

Joined: Tue Nov 18, 2008 5:01 am
Posts: 11
Hi all,

i have a OneToMany relationship in an @Entitiy

Code:
   @OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)
   @JoinColumn(name="transaction_id")
         private Set<MyData> myData= new HashSet<MyData>();


When i want to read this HashSet with:
Code:
myData= (HashSet<MyData>) MyData.getMyData();


i'm getting following exception:

java.lang.ClassCastException: java.util.HashSet cannot be cast to org.hibernate.collection.PersistentSet

i could use
Code:
PersistentSet persSet = (PersistentSet)myData.getMyData();
            if (myData.wasInitialized()) {
               myData.addAll(persSet);
            }


but i dont want to use hibernate classes in my code because i want to use pure JPA.

Does anyone have an idea why i have to cast and how to avoid this situation

thank you very much


Top
 Profile  
 
 Post subject: Re: CastException from Database to a HashSet
PostPosted: Fri May 21, 2010 1:41 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You should use Set, List, Map, etc. instead of concrete implementations.


Top
 Profile  
 
 Post subject: Re: CastException from Database to a HashSet
PostPosted: Fri May 21, 2010 2:53 pm 
Newbie

Joined: Tue Nov 18, 2008 5:01 am
Posts: 11
should i use the code like this?

@OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="transaction_id")
private Set<MyData> myData= new Set<MyData>();


or what do you mean???


Top
 Profile  
 
 Post subject: Re: CastException from Database to a HashSet
PostPosted: Fri May 21, 2010 5:21 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You can't do new Set() because that is an interface.

Here you should use Set and not HashSet (or PersistentSet):

Code:
myData= (HashSet<MyData>)MyData.getMyData();


Top
 Profile  
 
 Post subject: Re: CastException from Database to a HashSet
PostPosted: Sun May 23, 2010 8:08 am 
Newbie

Joined: Tue Nov 18, 2008 5:01 am
Posts: 11
Hi nordborg,

yes therefore i was asking, i didn't understand and couldn'T believe. I know that i can't create an instance from an interface....
i'll try it outan will reply later.
thank you very much

maxpade


Top
 Profile  
 
 Post subject: Re: CastException from Database to a HashSet
PostPosted: Tue May 25, 2010 4:27 am 
Newbie

Joined: Tue Nov 18, 2008 5:01 am
Posts: 11
Ok now it's working:

Set myData= MyData.getMyData();


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