openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Riccardo <riccardo.nim...@s2000.tu-chemnitz.de>
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:

@Entity
public class User implements Serializable {
   @Id
   private Long id;
	
   @Basic
   private String name;

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

   ...
}

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

   ...
}

@Entity
public class Item implements Serializable {
   @Id
   private Integer id;
   
   @Basic
   private String name;
   
   @Basic
   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
available.

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
satement
   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, 
		t0.Visibility 
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 
WHERE ...

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.


Regards

Riccardo



--
View this message in context: http://openjpa.208410.n2.nabble.com/Problem-with-Criteria-API-and-fetch-properties-of-related-entities-are-NULL-tp7582995.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Mime
View raw message