-->
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: ClassCastException with uniqueResult
PostPosted: Wed Sep 24, 2008 4:54 am 
Newbie

Joined: Wed Sep 24, 2008 4:39 am
Posts: 3
Hi Folks,

I'm new on the topic Hibernate and I have got a problem.
I've got a criteria on by database and i want to get a String with the help of uniqueResult. But I always get a ClassCastException and I do not know how to solve my Problem. Here my Code:

Code:

public static String getMitarbeiterId(String projektId, String projekt) {
        Session session = HibernateUtil.getCurrentSession();
        Criteria c = session.createCriteria(ProjektPlaner.class);
        c.add(Restrictions.eq("id", projektId));
        c.add(Restrictions.eq("projekt", projekt));
        c.add(Restrictions.eq("planer", Boolean.TRUE));
        c.setMaxResults(1);
       
        return (String) c.uniqueResult();
       
       
    }


Thanks for help.

heidi


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 5:19 am 
Beginner
Beginner

Joined: Mon Jul 05, 2004 9:29 am
Posts: 38
the return value of uniqueResult() will be of type ProjektPlaner and not String:

Code:
return (ProjektPlaner) c.uniqueResult();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 5:23 am 
Newbie

Joined: Wed Sep 24, 2008 4:39 am
Posts: 3
And how can I cast it to a String then?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2008 5:37 am 
Beginner
Beginner

Joined: Mon Jul 05, 2004 9:29 am
Posts: 38
what is it you want to return? a property from ProjektPlanner?

Code:
ProjektPlanner planner = (ProjektPlanner) c.uniqueResult();
return planner == null ? null : planner.getMyPropertyWhichIsAString();


but I don't think it is really efficient to do it that way: if later you need something else from ProjektPlanner, will you add a second method for that property? why not just return the ProjektPlanner and let the caller take what it needs from it.


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.