-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: NPE when combining fetch graphs and collection parameter
PostPosted: Thu Nov 06, 2014 5:50 pm 
Newbie

Joined: Wed Jul 23, 2014 4:08 am
Posts: 1
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.