[
https://issues.apache.org/jira/browse/OPENJPA-2150?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Anthony Fryer updated OPENJPA-2150:
-----------------------------------
Attachment: jpa-embeddedid-test.zip
This is a maven project that contains 2 test cases that demonstrates the issue.
The first test case uses a string query which correctly returns 1 row. The
second creates an identical CriteriaQuery which incorrectly returns 2 rows
because the generated SQL is missing one of the columns from the WHERE clause.
> 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
> Attachments: jpa-embeddedid-test.zip
>
>
> 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