ninada / full-hibernate-plugin-for-struts2

Automatically exported from code.google.com/p/full-hibernate-plugin-for-struts2
1 stars 0 forks source link

Session not Injected on validate() #40

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
In case of using Dao in validate process, the hibernate plugin doesn't inject 
Session (and i think Transaction. 

Here is a Sample to reproduce the problem:

UserDaoImpl:
-----------

public class UserDaoImpl implements UserDao {

    @SessionTarget
    Session session;

    @TransactionTarget
    Transaction transaction;

    @Override
    public User getUser(Long id) {
            if (session==null) {
                System.out.println("Session is null");
            }
        // TODO Auto-generated method stub
               User user = null;
        try {

            Criteria criteria = session.createCriteria(User.class).add(
                    Restrictions.eq("id", id_id));
            user = (User) criteria.uniqueResult();

        } catch (Exception e) {
            transaction.rollback();
            e.printStackTrace();
        }

        return user;
    }

-----

UserAction:
------
public class UserAction extends ActionSupport {

    private UserDAO userDao = new UserDaoImpl();
    private User user;

public String execute() {
 return "success";
}

public void validate() {
        clearFieldErrors();
                user  = userDao.getUser(1);
 ....

}
....
------------------

The Dao prints out "Session is null". NOTE: this is only sample code!

Original issue reported on code.google.com by lfelice...@softatnet.net on 14 Dec 2012 at 10:17

GoogleCodeExporter commented 8 years ago
Have you tried putting your Dao request into the execute()-method? That's 
pretty much where I keep the database interaction.

Probably your validate()-method gets invoked before the session is injected. 
This might be the reason, why your session is null (and not "closed").

Original comment by alexande...@googlemail.com on 7 Mar 2013 at 11:34

GoogleCodeExporter commented 8 years ago
In  the execute method it works, in validation the session remains null,  in 
all cases.

Original comment by leonardo...@gmail.com on 7 Mar 2013 at 11:43