I get a NullPointerException when executing the list method on a hibernate query. This is my code:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
String hql = "SELECT g FROM DeviceGroup g JOIN FETCH g.devices";
Query query = session.createQuery(hql);
List<DeviceGroup> list = query.list();
session.getTransaction().commit();
session.close();
return list;
The relationship is defined like this:
Device:
@ManyToOne
@JoinColumn
private DeviceGroup deviceGroup;
DeviceGroup:
@OneToMany(mappedBy = "deviceGroup")
private Set<Device> devices;
If I leave out the fetch join, the list method succeeds, but lazy fetching results in a StackOverflowException. What am I doing wrong?