users@glassfish.java.net

How to create EQL with aggregation function count, SELECT new DTO

From: <glassfish_at_javadesktop.org>
Date: Fri, 16 Feb 2007 11:46:41 PST

Hi!

My Entity Person has List<Person> friends;
My DTO PersonDTO has Long countOfFriends.
How can I create EQL to obtain PersonDTO?

See my example:
[b]1. Entity - entity.Person[/b]
public class Person implements Serializable{
    @Id()
    private int id;
    private String firstname;
    private String surname;
    
    @ManyToMany(fetch=FetchType.LAZY) // friends are LAZY initialized
    private List<Person> friends;

[b]2. DTO - dto.PersonDTO[/b]
public class PersonDTO implements Serializable{
    private int id;
    private String firstname;
    private String surname;
    private Long countOfFriends; // instead of List<Person>

    public PersonDTO(int id, String firstname, String surname, Long countOfFriends){ // fullconstructor

[b]3. I need something like this[/b]

[b]SELECT new dto.PersonDTO(res.id,res.firstname,res.surname,count(f))
FROM Person res LEFT JOIN res.friends f[/b]
(It does not work. There should be group by...)

It works if person has at least one friend:

[b]SELECT res.id, res.firstname, res.surname, count(f)
FROM Person res JOIN res.friends f
WHERE res.id=:id GROUP BY res.id, res.firstname, res.surname[/b]

If I used [b]LEFT JOIN[/b] instead [b]JOIN[/b],
it throws exception every time (even if person has more friends):

java.lang.NullPointerException
        at oracle.toplink.essentials.internal.expressions.SQLSelectStatement.appendFromClauseForOuterJoin(SQLSelectStatement.java:347)
        at oracle.toplink.essentials.internal.expressions.SQLSelectStatement.appendFromClauseToWriter(SQLSelectStatement.java:452)
        at oracle.toplink.essentials.internal.expressions.SQLSelectStatement.printSQL(SQLSelectStatement.java:1310)
        at oracle.toplink.essentials.internal.expressions.SQLSelectStatement.buildCall(SQLSelectStatement.java:683)
        at oracle.toplink.essentials.descriptors.ClassDescriptor.buildCallFromStatement(ClassDescriptor.java:550)


It is easy - to write it in SQL.
Can you help me, how to write it in EQL?

I can send source code with Junit tests.

Fafi
[Message sent by forum member 'fafi' (fafi)]

http://forums.java.net/jive/thread.jspa?messageID=204104