简体   繁体   English

NullPointerException与Hibernate FETCH JOIN

[英]NullPointerException with Hibernate FETCH JOIN

I get a NullPointerException when executing the list method on a hibernate query. 在休眠查询上执行list方法时,出现NullPointerException异常。 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. 如果我省略了获取联接,则list方法会成功,但是延迟获取会导致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. 我在导致无限循环的实体上执行了hashCode实现。 It's still quite a strange behavior to throw a NullPointerException in this case... 在这种情况下抛出NullPointerException还是很奇怪的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM