Ok, the problem is you have closed the Hibernate session here:
session.close();
When you call getUsers(), the collection of related users is lazily loaded. However, this can only be done within an active Hibernate session (which you don't have).
For lazy loading, there is some decent answers from BalusC and Pascal Thivent here:
What is lazy loading in Hibernate?
It is never a good idea to open and close sessions within a single DAO method. It is always better to do this in the tier above (usually referred to as the business/service tier).
Change your DAO class to this:
public class UnitsDaoImpl implements UnitsDao {
private Session session;
public UnitsDaoImpl(final Session session) {
this.session = session;
}
@Override
public List<Units> fetchAll() {
return session.createQuery("FROM Units").list();
}
// Other methods...
}
Change your calling code to this:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
UnitsDao dao = (UnitsDao)new UnitsDaoImpl(session);
List<Units> unity = (List<Units>)dao.fetchAll();
Users user = unity.get(0).getUsers();
System.out.println(user);
session.getTransaction().commit();
session.close();