-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem with the find method of HibernateTemplate
PostPosted: Mon May 24, 2010 11:44 am 
Newbie

Joined: Fri Apr 18, 2008 1:07 am
Posts: 14
Location: Pakistan
Hi,

I am having some problem with the Hibernate + Spring testing framework. I have written a test that saves a record in a transaction. When I use the get method of HibernateTemplate, it works fine but when I try to execute the find method. It does not return the object that I saved in the same transaction. Below is my method that saves the user record in a transaction.

Code:

User newUser = new User();
                user.setLoginName("kashif.nazar");
      dao.saveUser(newUser);
      userId = newUser.getUserId();






and below are the methods that I am using to retrieve the saved object.



Code:
  // this test is working fine
       public void testGetUser() throws Exception {
      User user = dao.getUser(userId);
      assertNotNull(user);
      assertEquals("kashif.nazar", user.getLoginName());
   }

       // this test is NOT working fine
            public void testGetUserByLoginName() throws Exception {
      User user = dao.getUserByLoginName("kashif.nazar");
      assertNotNull(user);
   }


As you can see in the above comment, the second method does not pass the test because the user object returned is null. Here is the definition of the method, getUserByLoginName.

Code:

public User getUserByLoginName(String loginName) {
            @SuppressWarnings("unchecked")
            List<User> users = getHibernateTemplate().
                find("from User u where u.loginName = ?",
                    new Object[]{loginName});
            if (users != null && users.size() > 0) {
                return users.get(0);
            }
            return null;
        }



Please help me.

Regards,
Kashif

_________________
Muhammad Kashif Nazar
Software Engineer


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.