openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Riccardo <>
Subject Problem with Criteria-API and fetch (properties of related entities are NULL)
Date Tue, 05 Mar 2013 11:24:43 GMT
Hi folks,

I am using the Criteria-API to find Entities matching some more complicated
conditions. My (simplified) Entities look like the following code:

public class User implements Serializable {
   private Long id;
   private String name;

   private String avatar;
   private String profile;
   @OneToOne(mappedBy = "owner")
   private Inventory inventory;


public class Inventory implements Serializable {
   @OneToOne(fetch = FetchType.EAGER)
   @JoinColumn(name = "Owner", referencedColumnName = "Id")
   private User owner;
   private int slots;
   private Visibility visibility;
   @OneToMany(cascade = {CascadeType.ALL}, orphanRemoval = false)
       name = "Ownership",
       joinColumns = {
             @JoinColumn(name = "Inventory", referencedColumnName = "Owner",
unique = false)
       inverseJoinColumns = {
             @JoinColumn(name = "Item", referencedColumnName = "Id", unique
= true)
   private Set<Item> content;


public class Item implements Serializable {
   private Integer id;
   private String name;
   private String description;


Note that I use use a mapping of derived identities for the relation
Inventory <-> User. Based on this, I try to execute a query with a fetch
using the Criteria-API to retrieve inventories, to have the associated user

private List<Inventory> findInventories(...) {
   CriteriaBuilder builder = this.manager.getCriteriaBuilder();
   CriteriaQuery<Inventory> query = builder.createQuery(Inventory.class);
   Root<Inventory> inventory = query.from(Inventory.class);
   // This should also load the user data to avoid an additional SELECT
   Fetch<Inventory, User> owner = inventory.fetch("owner", JoinType.INNER);


   TypedQuery<Inventory> typedQuery = this.manager.createQuery(query);
   return typedQuery.getResultList();

I omited the WHERE-clause, to keep things simple.

The trace shows me, that the resulting SQL query (especially the
SELECT-claue) looks like expected.

SELECT t0.Owner, t3.Id, t3.Avatar, t3.Name, t3.Profile, t0.Slots, 
FROM Inventories t0 INNER JOIN Ownership t1 ON t0.Owner = t1.Inventory 
        INNER JOIN Users t3 ON t0.Owner = t3.Id INNER JOIN Items t2 
        ON t1.Item = t2.Id 

However, if I debug the code, I can see, that the properties of the
User-objects are all NULL except the id. This results in an additional
SELECT if I try to access these properties. On the other hand I added an
additional fetch (Join<Inventory, Item> content = inventory.fetch("content",
JoinType.INNER)) to also load the content of the inventories which works as
expected. Here all the properties of the items are initialized with the
actual values.

Since the second part works as expected, I assume a bug if I use a mapping
of derived identities and any feedback to my problem would be welcome.



View this message in context:
Sent from the OpenJPA Users mailing list archive at

View raw message