0

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?

0

2 Answers 2

2

Try with identifier d on g.devices ,

SELECT g FROM DeviceGroup g JOIN FETCH g.devices d
Sign up to request clarification or add additional context in comments.

Comments

1

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...

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.