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?
尝试在g.devices
上使用identifier d
,
SELECT g FROM DeviceGroup g JOIN FETCH g.devices d
I solved the problem. I had a hashCode implementation on the entities that caused an infinite loop. It's still quite a strange behavior to throw a NullPointerException in this case...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.