Playframework sends 2 queries for fetched query
        Posted  
        
            by 
                MRu
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by MRu
        
        
        
        Published on 2012-10-24T16:30:05Z
        Indexed on 
            2012/10/24
            17:00 UTC
        
        
        Read the original article
        Hit count: 281
        
I currently have problems with the JPA at the play framework 1.2.4.
I need to have a UserOptions model in a separate database and want to join it lazyly cause its only needed in one query.
In this query I want to load the options eagerly and by searching I found out that can only be done by using a join query.
If I use eager instead oder lazy, everything would be fine by using User.findById() and the options and the user is found in one query.
But play sends two queries when I use a 'left join fetch' query. So heres the query:
User.find("
    SELECT
        user
    FROM
        User user
    LEFT JOIN FETCH
        user.options options
    WHERE
        user.id = ?
", Long.parseLong(id)).first();
And here the models:
@Entity
public class User extends Model
{
    @OneToOne(mappedBy = "user", fetch = FetchType.LAZY)
    public UserOptions  options;
    // ...
}
@Entity
public class UserOptions extends Model
{
    @OneToOne(fetch = FetchType.LAZY)
    public User user;
}
The question is why play sends two query for the fetch query?
Thanks in advance
© Stack Overflow or respective owner