Announcement Announcement Module
Collapse
No announcement yet.
Where's HibernateDaoSupport.load(Object,Serializable)? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Where's HibernateDaoSupport.load(Object,Serializable)?

    HibernateDaoSupport only has a load(Class,Serializable) method, but Hibernate's session has a load(Class,Serializable) and load(Object,Serializable). I would like to do this:

    Code:
    public void load(User user) throws Exception {
            getHibernateTemplate().load(user,user.getId());
        }
    For some reason, this doesn't work (user ends up being null):

    Code:
    public void load(User user) throws Exception {
            user = (User)getHibernateTemplate().load(user.getClass(),user.getId());
        }
    This works:

    Code:
    public void load(User user) throws Exception {
            getSessionFactory().openSession().load(user,user.getId());
        }

  • #2
    Indeed, HibernateTemplate lacks load(Object, Serializable). Please add a request on JIRA web site.

    public void load(User user) throws Exception {
    user = (User)getHibernateTemplate().load(user.getClass(), user.getId());
    }
    There is a problem with this method, user is a reference to an Object and should not be assigned. You can use:
    Code:
      public User load(Serializable id) throws DataAccessException { 
            return (User)getHibernateTemplate().load(User.class, id); 
        }
    HTH

    Comment


    • #3
      Ok, I'l post the bug about load(Object,Serializable) missing.

      On the second issue, what's wrong with populating the object passed in to the method? Say that void load(User) is a method on a dao. The client code would look like this:

      User user = new User();
      user.setId(new Long(1));
      dao.load(user);

      Is that a problem? I guess I could do this, as you suggest:

      User user = new User();
      user.setId(new Long(1));
      user = dao.load(user);

      Comment


      • #4
        Ok, I'l post the bug about load(Object,Serializable) missing.
        I think this is more likely an enhancement

        On the second issue, what's wrong with populating the object passed in to the method? Say that void load(User) is a method on a dao. The client code would look like this:

        User user = new User();
        user.setId(new Long(1));
        dao.load(user);

        Is that a problem?
        Well, this has nothing to do with Spring / Hibernate, This is a Java issue
        dao.load(user) call send a reference to its user object to method load. If method load assigns a new user to this reference user = newUser; the calling method can not see newUser.

        I hope the following sample makes it clearer:
        Code:
        public class Taha {
        	public Taha() {
        		String initial = "My son 1";
        		System.out.println (initial);
        		oneMethod(initial);
        		System.out.println (initial);
        	}
        
          public void oneMethod(String param) {
          	param = "My son 2";
          	System.out.println (param);
          }
        
        	public static void main (String[] args) {
        		new Taha();
        	}
        }
        output
        Code:
        My son 1
        My son 2
        My son 1
        HTH

        Comment


        • #5
          You are right, I understand your point about setting the parameter equal to a a reference of an object that it cannot see. Thanks.

          Comment


          • #6
            Ok, I'l post the bug about load(Object,Serializable) missing.
            It is actually implemented in Spring cvs.
            Thank you Juergen.

            Comment

            Working...
            X