views:

243

answers:

3

Hello people! hope everybody is cool.I've been trying to do something with spring-hibernate but it's still a failure.i'm very new to it.i need a little help.supposing a POJO class Users.i want to return an Users object by username.While i had no problem to return Users object by id by doing this

   return (Users) getHibernateTemplate().get(Users.class, id);

it seems to be a challenge for me getting the Users object by username. I've tried this

List<Users> users = getHibernateTemplate().find("from Users u where u.username=?",username);
return  users.get(0);

and even this

return (Users)getHibernateTemplate().find("from Users u where u.username=?",username).get(0);

how should i do it.And what is the proper way to return an array of Users objects and/or List.Thanks for reading this.

+1  A: 

maybe you can try this?

List l =getHibernateTemplate().find("from Users u where u.username=?",new Object[]{username});
if (l.size() > 0)
 return (User)l.get(0);
else
return null;
ufukgun
Please not the dreaded 'return null' - that's asking for a NullPointerException somewhere shortly after the method containing the above code is called. My prefered way to deal with this situation would be to throw a UserNotFoundException.
Nick Holt
I don't think you can generalise like that without knowing the context of the operation. Returning a null may be defined by interface contract for all we know.
skaffman
i returned a new users object.thanks dudes.What about arrays of Users? supposing i want to return all the users of the name francis?
black sensei
And how about all the users name 'james' :). Of course return the list, instead of the first item. Where is the problem? By the way, I am not in the favor of writing SQL for trivial cases while using Hibernate kind of powerful API.
Adeel Ansari
Ah and in case no user found return an empty list :), instead of throwing exception or returning null. A nice idea indeed, depends on the interface contract though. Cheers.
Adeel Ansari
yeah :) but i need to start from somewhere before i get to that complexity.i wasn't developing in java before but C# and was using code author.I learn much better when i try things myself.thanks
black sensei
+2  A: 

Try this

Query q = session.createQuery("select * from Users u where u.username=:username");
q.setString("username", username);
Adeel Ansari
This is also my preferred way of handling this. I've found that naming my parameters makes it easier to understand what the code is doing.
CaptainAwesomePants
+1  A: 

Another better way is to use Criteria. See the example below.

    DetachedCriteria deCriteria = DetachedCriteria.forClass(User.class, "u");
    Criteria criteria = deCriteria.getExecutableCriteria(session);

    criteria.add(Restrictions.eq("u.username", username));
    List<User> userList = criteria.list();
Adeel Ansari
i'll try this but i think i'll try it with the spring function findbycriteria and see how it will go along.thanks
black sensei
You must, its not encouraged to write SQL for trivial cases while using Hibernate/Spring kind of powerful APIs.
Adeel Ansari