openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anthony Fryer (Created) (JIRA)" <j...@apache.org>
Subject [jira] [Created] (OPENJPA-2150) CriteriaQuery misses a join column in generated sql for queries using @EmbeddedId fields
Date Mon, 05 Mar 2012 23:25:57 GMT
CriteriaQuery misses a join column in generated sql for queries using @EmbeddedId fields
----------------------------------------------------------------------------------------

                 Key: OPENJPA-2150
                 URL: https://issues.apache.org/jira/browse/OPENJPA-2150
             Project: OpenJPA
          Issue Type: Bug
          Components: criteria
    Affects Versions: 2.2.0
         Environment: windows 7 32 bit
java 6
            Reporter: Anthony Fryer


I am noticing a problem when using criteria queries against entities that use an @EmbeddedId
that doesn't occur when using a string based query.

To summarise, the string query below..

em.createQuery("select lrm.user from LeagueRoleMember as lrm where lrm.leagueRole = :leagueRole")
                                .setParameter("leagueRole", leagueRole)
                                .getResultList();

will execute the following sql statement...

SELECT t1.USER_NAME FROM LeagueRoleMember t0 LEFT OUTER JOIN User t1 ON t0.USER_NAME = t1.USER_NAME
WHERE (t0.LEAGUE_ID = ? AND t0.ROLE_NAME = ?)

However, a CriteriaQuery constructed to be identical to the String query...

CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<User> cq = cb.createQuery(User.class);
Root<LeagueRoleMember> leagueRoleMember = cq.from(LeagueRoleMember.class);
cq.select(leagueRoleMember.get(LeagueRoleMember_.user));
cq.where(cb.equal(leagueRoleMember.get(LeagueRoleMember_.leagueRole), cb.parameter(LeagueRole.class,
"leagueRole")));
em.createQuery(cq).setParameter("leagueRole", leagueRole).getResultList();

Will execute this sql statement (NOTE: it doesn't use t0.ROLE_NAME in the where clause which
it should)...

SELECT t1.USER_NAME FROM LeagueRoleMember t0 LEFT OUTER JOIN User t1 ON t0.USER_NAME = t1.USER_NAME
WHERE (t0.LEAGUE_ID = ?)

The entities are mapped as follows (getters and setters omitted)...

@Entity
public class User {
        @Id
        @Column(name="USER_NAME")
        private String userName;
}

@Entity
public class League  {
        @Id
        private Integer id;
       
        @Column(name="NAME")
        String name;
}

@Embeddable
public class LeagueRolePK implements Serializable {
        @Column(name="LEAGUE_ID")
        private Integer leagueId;
       
        @Column(name="ROLE_NAME")
        private String roleName;
}

@Entity
public class LeagueRole {
        @EmbeddedId LeagueRolePK id;

        @MapsId("leagueId")
        @ManyToOne
        private League league;
}

@Embeddable
public class LeagueRoleMemberPK implements Serializable {
        LeagueRolePK leagueRolePK;
       
        @Column(name="USER_NAME")
        private String userName;
}

@Entity
public class LeagueRoleMember implements Serializable {
        @EmbeddedId LeagueRoleMemberPK id;
       
        @MapsId("leagueRolePK")
        @ManyToOne
        LeagueRole leagueRole;
       
        @MapsId("userName")
        @ManyToOne
        User user;
} 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message