0

I am new in hibernate and I am struggling with a problem. I want to display a list of objects to a .XHTML page using JSF. But I don't know why, I lose the data on the way. Even though the method from DAO returns a list of objects from database (I saw it by doing debug) when I try to assign that list to a list of users from another class I loose that data and the list from DAO becomes null. So there are no results in GUI from my datatable.

public class UsersBean {

        private List<User> allUsers;

        public UsersBean() {
        init();
        }

        private void init() {
        UsersController.doInitialise(allUsers);
        }

  // getters, setters

    }


public class UsersController {

    public static void doInitialise(List<User> users) {

    users = new ArrayList<User>();
    UserDao userDao = new UserDaoImpl();
    users = userDao.getAllEnities();
    System.out.println(users.toString());
    }

}

public class UserDaoImpl{

    @Override
    public List<User> getAllEnities() {

    List<User> users= null;
    Session session = null;
    Transaction transaction = null;
    try {
        session = sessionFactory.openSession();
        transaction = session.beginTransaction();
        org.hibernate.query.Query query = session.createQuery("from User");
        users= query.list();
        transaction.commit();
    }  finally {
        session.close();
    }
    return users;
    }

}
1
  • I would assume it could be because you're closing the session. Once you close the session all the references to those objects become null. With Hibernate you generally keep a single session open until you're done with the objects that are cached in that session. Usually a single session is enough for most applications. Commented Mar 6, 2020 at 13:44

2 Answers 2

2

If you want to modify the content of the original list, don't assign another list object to it, this will have no effect.

Instead, just alter the content directly, i.e :

public static void doInitialise(List<User> users) {

UserDao userDao = new UserDaoImpl();
users.addAll(userDao.getAllEnities());
System.out.println(users.toString());

}
Sign up to request clarification or add additional context in comments.

Comments

1

You are not assigning the value of allUsers into your UsersBean, probably that's why is not working. To have it working, refactor the doInitialise to return a List<User>, then, in the UsersBean, just assign the users.

class UsersBean {

    private List<User> allUsers;

    public UsersBean() {
        init();
    }

    private void init() {
        this.allUsers = UsersController.doInitialise();
    }
}
public class UsersController {

    public static List<User> doInitialise() {
        UserDao userDao = new UserDaoImpl();

        return userDao.getAllEnities();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.