Results 1 to 6 of 6

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

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3

    Default

    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);

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5

    Default

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

  6. #6
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

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

    Spring Modules Team
    http://irbouh.blogspot.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •