spkhillar / hibernate-generic-dao

Automatically exported from code.google.com/p/hibernate-generic-dao
0 stars 0 forks source link

save() doesn't get the id of the entity #45

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Caused by: java.lang.NullPointerException
        at
com.trg.dao.hibernate.HibernateBaseDAO._saveOrUpdateIsNew(HibernateBaseDAO.java:
158)
        at com.trg.dao.hibernate.GeneralDAOImpl.save(GeneralDAOImpl.java:87)
        at model.db.dbOperations.changeUserPass(dbOperations.java:67)

the method save was called in this way:

 public static boolean changeUserPass(String username, String newPass) {
           UniversalDAO dao=new UniversalDAO();
           User u=dao.find(User.class, username);
           u.setPassword(utils.CryptoUtils.hexMD5(newPass));
           dao.save(u);
           return true;
    }

and the entity is mapped by annotations:

     @Id 

    @Column(name="username", unique=true, nullable=false, length=20)
    public String getUsername() {
        return this.username;
    }

in Hibernate General DAOs why don't you use the native SaveOrUpdate()
method of Hibernate Session for this purpose?

in my app i resolved in this way (waiting for your bugfix)

/**
 *
 * @author stefano
 */
public class UniversalDAO extends GeneralDAOImpl {

    @Override
    protected Session getSession() {
            return SessionManager.currentSession();
    }

    public boolean save2(Object entity) {
        Session sess=getSession();
        Transaction tx = sess.beginTransaction();
        tx.begin();
        sess.saveOrUpdate(entity);
        tx.commit();
        return true;
    }

}

Original issue reported on code.google.com by rastrano on 25 Aug 2009 at 12:09

GoogleCodeExporter commented 9 years ago
I think the cause of your null pointer exception is that you need to set the 
SessionFactory on the DAO.

You do bring up a good question why we have our own version of save or update. 
It is 
to that we can return the boolean value of whether it was a new or existing 
entity. 
However, I don't know if anyone really would use that information anyway. I 
would 
love to hear what the user community has to say about it. If no one finds it 
helpful, 
we might as well get rid of it. But in any case, you should still specify a 
SessionFactory on the DAO to make everything work properly.

Thanks for the bug report.

Original comment by dwolvert on 25 Aug 2009 at 5:08

GoogleCodeExporter commented 9 years ago
The overriding the getSession() method is not a right method to set the 
sessionfactory in GeneralDAOImpl?

can you provide me a short example of the best pratice to set it?

anyway I think that the boolean return value yuo're talking about is a very 
useful 
thing (I always was asking myself why do Hibernate don't implemented this...)

Compliments for the great work! 
best regards,
Stefano.

Original comment by rastrano on 4 Dec 2009 at 12:30

GoogleCodeExporter commented 9 years ago
I afound the answer to latter question in your documentation:

Each DAO and the SearchFacade needs both a Session and a SessionFactory in 
order to 
work. By default they expose a public setSessionFactory() method and use 
SessionFactory.getCurrentSession() to get a Session whenever they need it. With 
this 
configuration the SessionFactory is required and is generally set once when the 
object is initialized.

thank you.
Stefano.

Original comment by rastrano on 4 Dec 2009 at 12:36

GoogleCodeExporter commented 9 years ago
org.hibernate.QueryException: undefined alias: id

from this code

dao.save(entity);

and solved by this

dao._saveOrUpdate(entity);

So why don't we modify the following two methods to use saveOrUpdate()?

        public boolean save(Object entity) {
                return _saveOrUpdateIsNew(entity);
        }

        public boolean[] save(Object... entities) {
                return _saveOrUpdateIsNew(entities);
        }

Modify to

        public boolean save(Object entity) {
                return _saveOrUpdate(entity);
        }

        public boolean[] save(Object... entities) {
                return _saveOrUpdate(entities);
        }

Original comment by ngpeij...@gmail.com on 3 May 2012 at 8:47