You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Riccardo <ri...@s2000.tu-chemnitz.de> on 2013/03/05 12:24:43 UTC

Problem with Criteria-API and fetch (properties of related entities are NULL)

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.

RE: Problem with Criteria-API and fetch (properties of related entities are NULL)

Posted by Boblitz John <jo...@bertschi.com>.
Hello,

This may have already been addressed,  but have you looked at you settings for Fetch Groups and Eager Fetching?  (Part III , 5.7 and 5.8)


John

> -----Original Message-----
> From: Riccardo [mailto:riccardo.nimser@s2000.tu-chemnitz.de]
> Sent: Wednesday, March 06, 2013 8:31 PM
> To: users@openjpa.apache.org
> Subject: Re: Problem with Criteria-API and fetch (properties of related
> entities are NULL)
> 
> I prefer not to change my database schema, but I found another workaround
> which works. For this I use the annotation MapsId on the property "owner"
> and an additional property "id". (JSR 317, Section 2.4.1.3, Example 4b)
> 
> @Entity
> public class Inventory implements Serializable {
>    @Id
>    private Long id;
> 
>    @MapsId
>    @OneToOne(fetch = FetchType.EAGER)
>    @JoinColumn(name = "Owner", referencedColumnName = "Id")
>    private User owner;
> 
>    ...
> }
> 
> With these it works as expected, without the need of changing my database
> schema. The only property which is still NULL in the user is the "inventory",
> which means that the opposite direction of the relation Inventory <-> User is
> not set, which is still strange for me.
> 
> 
> 
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Problem-with-Criteria-API-and-fetch-
> properties-of-related-entities-are-NULL-tp7582995p7583037.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Problem with Criteria-API and fetch (properties of related entities are NULL)

Posted by Riccardo <ri...@s2000.tu-chemnitz.de>.
I prefer not to change my database schema, but I found another workaround
which works. For this I use the annotation MapsId on the property "owner"
and an additional property "id". (JSR 317, Section 2.4.1.3, Example 4b)

@Entity
public class Inventory implements Serializable {
   @Id
   private Long id;

   @MapsId
   @OneToOne(fetch = FetchType.EAGER)
   @JoinColumn(name = "Owner", referencedColumnName = "Id")
   private User owner;

   ...
}

With these it works as expected, without the need of changing my database
schema. The only property which is still NULL in the user is the
"inventory", which means that the opposite direction of the relation
Inventory <-> User is not set, which is still strange for me.



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

Re: Problem with Criteria-API and fetch (properties of related entities are NULL)

Posted by José Luis Cetina <ma...@gmail.com>.
mmm i never used that before, why you dont consider this:

In your inventory table just create a primary key (numeric) and then add
the reference of your User in this table (you can use a unique constraint
if you dont want duplicates users).


2013/3/6 Riccardo <ri...@s2000.tu-chemnitz.de>

> With JPQL and the Criteria-API I get the exact same result.
>
> Other Entities work for me too, but in this case I use a mapping of derived
> identities as specified in JSR 317 section 2.4.1. So, the primary key for
> my
> inventories is another entity (User).
>
> @Entity
> public class Inventory implements Serializable {
>    @Id
>    @OneToOne(fetch = FetchType.EAGER)
>    @JoinColumn(name = "Owner", referencedColumnName = "Id")
>    private User owner;
>
>    ...
> }
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Problem-with-Criteria-API-and-fetch-properties-of-related-entities-are-NULL-tp7582995p7583035.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: Problem with Criteria-API and fetch (properties of related entities are NULL)

Posted by Riccardo <ri...@s2000.tu-chemnitz.de>.
With JPQL and the Criteria-API I get the exact same result.

Other Entities work for me too, but in this case I use a mapping of derived
identities as specified in JSR 317 section 2.4.1. So, the primary key for my
inventories is another entity (User). 

@Entity
public class Inventory implements Serializable {
   @Id
   @OneToOne(fetch = FetchType.EAGER)
   @JoinColumn(name = "Owner", referencedColumnName = "Id")
   private User owner;

   ...
}



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

Re: Problem with Criteria-API and fetch (properties of related entities are NULL)

Posted by José Luis Cetina <ma...@gmail.com>.
this is really strange i use extensive the find method and i never seen
that problem.

What about if you use use criteria builder or jpql for replace the find
method and see what happen?


2013/3/6 Riccardo <ri...@s2000.tu-chemnitz.de>

> If I run the query, I get exactly the result I expect. If I use the
> find-Method the properties of the inventory are all filled with the right
> values, also the user-object has the right id, but the other properties of
> the user-object are just NULL.
>
> If I access the other properties of the user via a getter a new SELECT is
> executed by OpenJPA do load the values. So, everything works, but the
> additional SELECT is to much in my opinion, since this leads to 1+N
> selects,
> which is not necessary.
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Problem-with-Criteria-API-and-fetch-properties-of-related-entities-are-NULL-tp7582995p7583033.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: Problem with Criteria-API and fetch (properties of related entities are NULL)

Posted by Riccardo <ri...@s2000.tu-chemnitz.de>.
If I run the query, I get exactly the result I expect. If I use the
find-Method the properties of the inventory are all filled with the right
values, also the user-object has the right id, but the other properties of
the user-object are just NULL.

If I access the other properties of the user via a getter a new SELECT is
executed by OpenJPA do load the values. So, everything works, but the
additional SELECT is to much in my opinion, since this leads to 1+N selects,
which is not necessary.



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

Re: Problem with Criteria-API and fetch (properties of related entities are NULL)

Posted by José Luis Cetina <ma...@gmail.com>.
Run this query in your database and see if you have any results.


2013/3/6 Riccardo <ri...@s2000.tu-chemnitz.de>

> The SQL looks fine:
>
> SELECT
>    t1.Id, t1.Avatar, t1.Name, t1.Profile, t0.Slots, t0.Visibility
> FROM
>    Inventories t0 LEFT OUTER JOIN Users t1 ON t0.Owner = t1.Id
> WHERE
>    t0.Owner = ?
>
> All the properties annotated with @Basic in the classes User and Inventory
> are part of the SELECT-clause.
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Problem-with-Criteria-API-and-fetch-properties-of-related-entities-are-NULL-tp7582995p7583030.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: Problem with Criteria-API and fetch (properties of related entities are NULL)

Posted by Riccardo <ri...@s2000.tu-chemnitz.de>.
The SQL looks fine:

SELECT
   t1.Id, t1.Avatar, t1.Name, t1.Profile, t0.Slots, t0.Visibility 
FROM
   Inventories t0 LEFT OUTER JOIN Users t1 ON t0.Owner = t1.Id 
WHERE
   t0.Owner = ?

All the properties annotated with @Basic in the classes User and Inventory
are part of the SELECT-clause.



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

Re: Problem with Criteria-API and fetch (properties of related entities are NULL)

Posted by José Luis Cetina <ma...@gmail.com>.
If you use find you have to get all the properties, if you use getReference
you will only get the id IIRC.

Why you dont print you sql inserts to log file and see if the query is what
you expect


2013/3/6 Riccardo <ri...@s2000.tu-chemnitz.de>

> Ok, I just found out, that the properties of the owner (user) are also all
> NULL, if I just search an inventory by its primary key via:
>
>     this.manager.find(Inventory.class, someId);
>
> For me this seems not to be normal. Maybe one of the developers can give me
> some feedback to this problem.
>
> 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-tp7582995p7583024.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>



-- 
-------------------------------------------------------------------
*SCJA. José Luis Cetina*
-------------------------------------------------------------------

Re: Problem with Criteria-API and fetch (properties of related entities are NULL)

Posted by Riccardo <ri...@s2000.tu-chemnitz.de>.
Ok, I just found out, that the properties of the owner (user) are also all
NULL, if I just search an inventory by its primary key via:

    this.manager.find(Inventory.class, someId);

For me this seems not to be normal. Maybe one of the developers can give me
some feedback to this problem.

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-tp7582995p7583024.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.