I have two named queries, and both have the same logic, except that each one uses a different object constructor to select the results into
Named Query 1:
SELECT DISTINCT NEW dk.NewsRoomView(n,p)
FROM NewsItem n LEFT JOIN n.placements p JOIN n.actors a
Named Query 2:
SELECT DISTINCT NEW dk.NewsRoomView(n.id,n.title,SIZE(n.attachments),p.id,p.page)
FROM NewsItem n LEFT JOIN n.placements p JOIN n.actors a
The Structure of the NewsRoomView class
public class NewsRoomView{
public NewsRoomView(NewsItem n, NewsItemPlacement p)
Logger.getLogger(NewsRoomView.class.getName()).log(Level.INFO,"at Contsructor 1");
}
public NewsRoomView(Long id, String title, Integer attachments, Long pId, Integer page){
Logger.getLogger(NewsRoomView.class.getName()).log(Level.INFO,"at Contsructor 2");
}
}
Now whenever Named Query 1 gets executed it successfully prints the “at constructor 1” statement and thus the query returns results, but when Named Query 2 gets executed it neither prints out the statement “at Constructor 2”, nor returns results.
The code blocks above are only snippets of what I really have. In reality the second constructor has a 23 long argument list, that represents some more fields in the NewsItem and NewsItemPlacement entities.
I was wondering what could be wrong that prevents the second constructor from returning results, and whether there are limits on the size of the arguments list for the constructor.
Thanks in advance.
Because there is no exception thrown, it means that generated query was executed against database without problems, but query didn’t returned any rows. Problem has nothing to do with constructor expression and number of arguments.
I guess that there is no such a NewsItem that have entry in attachment table. Such a rows will not be part of result set, because SIZE(n.attachments) causes id based inner join to attachments table. This all comes much more clear if you take a look to the generated SQL queries.
Minimal example with same problem
Entities
Database content
Query
EclipseLink (2.3.2) with MySQL generates following SQL query:
As you see, it returns size of bList only for such an EntityA that does have one or more entries in EntityB table.