Hi all,
I am using JPA 2.1 and Hibernate 4.3.5 (the one that comes with Wildfly 8.1) and encounter a strange behaviour when i combine fetch graphs and a collection-valued parameter. I managed to pin-down the problem with a simple example.
The minimal entities are organized as follows (getters/setters are not shown here):
Code:
@Entity
public class Device {
@Id
private String id;
@OneToOne(fetch=FetchType.EAGER)
private DeviceState state;
}
...
@Entity
public class DeviceState {
@Id
private String id;
@OneToMany(fetch=FetchType.LAZY)
private List<Problem> problems;
}
...
@Entity
public class Problem {
@Id
private String id;
private String code;
private long appearedAt;
}
Now I want to execute a query which fetches a set of devices (by providing a list of ids), the corresponding states and the list of problems. This shall be manageable in one SQL query, so I used an appropriate fetch plan and tried it with one id first. This code snipplet works...
Code:
private List<Device> works(String deviceId) {
TypedQuery<Device> query = em.createQuery("SELECT d FROM Device d WHERE d.id = :id", Device.class);
query.setParameter("id", deviceId);
EntityGraph<Device> entityGraph = em.createEntityGraph(Device.class);
entityGraph.addAttributeNodes("state");
Subgraph<DeviceState> state = entityGraph.addSubgraph("state", DeviceState.class);
state.addAttributeNodes("problems");
query.setHint("javax.persistence.fetchgraph", entityGraph);
return query.getResultList();
}
However, when replacing the single-valued parameter with a collection, I get an NPE
Code:
private List<Device> doesntwork(String deviceId) {
TypedQuery<Device> query = em.createQuery("SELECT d FROM Device d WHERE d.id IN :ids", Device.class);
query.setParameter("ids", Collections.singletonList(deviceId));
EntityGraph<Device> entityGraph = em.createEntityGraph(Device.class);
entityGraph.addAttributeNodes("state");
Subgraph<DeviceState> state = entityGraph.addSubgraph("state", DeviceState.class);
state.addAttributeNodes("problems");
query.setHint("javax.persistence.fetchgraph", entityGraph);
return query.getResultList();
}
When having a look on the logger output of org.hibernate.SQL and .type, it shows me that the query is correctly built however, the parameter binding does not seem to work. Also, when i comment out the line that sets the query hint, the query works fine (of course without the desired prefetching).
I assume that this is a bug in the implementation. Are there any solutions to this problem?