|JPQL join and collection association [message #733915]
||Wed, 05 October 2011 19:23
Registered: November 2010
I have a simple JPQL query (below) and I have 2 entities, PrintDocumentsInfo (the owner) and PrintItemInfo (the reverse); I have the ManyToOne mapping in the PrintDocumentsInfoList entity and the OneToMany mapping in the PrintItemInfo entity.|
The fields and annotations are also below.
This query works. However, the Collection<PrintDocumentsInfo> in the PrintItemInfo class is not limited to PrintDocumentsInfo adviceStatus2 = 100.
Unfortunately, I get a Collection of all adviceStatus2 values whenever there is an adviceStatus2 == 100 in the any of the ForeignKeys that relate back to the PrintItemInfo table.
This seems so straightforward in the docs, tutorials, and books but no way can I get this to return the correct results. What am I missing?
Thanks in advance for you helpl
//the jpql join query
String jpqlString3 = "SELECT docs " +
"FROM PrintItemInfo info INNER JOIN info.printDocumentsInfoList docs " +
"WHERE docs.adviceStatus2 = 100 "
//this is the "owning" side of the relationship in the PrintDocumentsInfo class
private PrintItemInfo printItemInfo;
//this is the "inverse" side in the PrintItemInfo class
private Collection<PrintDocumentsInfo> printDocumentsInfoList
= new TreeSet<PrintDocumentsInfo>();
|Re: JPQL join and collection association [message #734108 is a reply to message #734078]
||Thu, 06 October 2011 13:37
| Chris Delahunt
Registered: July 2009
I believe what is being said is that the query is correctly returning docs that reference a docs.adviceStatus2 = 100. What is being stated is that the returned and so managed PrintItemInfo's 1:M collection of PrintDocumentsInfo is fully populated and not filtered based on the query. |
If so, this is as expected. The query is to give back fully populated PrintDocumentsInfo. All references will contain all the information associated to the entities as they exist in the database. So the PrintItemInfo's referenced from these returned PrintDocumentsInfo must be full populated and not use the criteria from the query, as doing so would corrupt the cache - these entities are managed so other queries in the same EntityManager context are expected to return the same instance.
My suggestion would be not access the PrintItemInfo's collection so that it remains untriggered, and just used the collection of PrintDocumentsInfo entities returned from the query directly, sorting them by their PrintItemInfo.
[Updated on: Thu, 06 October 2011 13:38]
Report message to a moderator
Powered by FUDForum
. Page generated in 0.10693 seconds