Export to GitHub

full-hibernate-plugin-for-struts2 - issue #40

Session not Injected on validate()


Posted on Dec 14, 2012 by Happy Giraffe

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!

Comment #1

Posted on Mar 7, 2013 by Grumpy Lion

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").

Comment #2

Posted on Mar 7, 2013 by Helpful Bear

In the execute method it works, in validation the session remains null, in all cases.

Status: New

Labels:
Type-Defect Priority-Medium