Hi,
I'm using hibernate 3.5.0-CR2 with MySQL server 5.1.44 (through mysql-connector-java-5.1.9) on Windows. I'm using JPA 2.0 and issuing a native query. The query runs fine when run directly agains the server using MySQL Browser. When I run it through JPA/Hibernate, the number of items returned is correct but the values returned for each item is not correct. I believe this is being caused by the fact that the column names being returned are the same.
My Object looks like this:
@Entity public class Relationship { @Id private Long id;
@ManyToOne @JoinColumn(name="supervisorRoleId") private Role supervisorRole; @ManyToOne @JoinColumn(name="subordinateRoleId") private Role subordinateRole;
... }
And I issue a query like this:
Query query = entityManager.createNativeQuery("SELECT r1.subordinateRoleId, r2.subordinateRoleId, r3.subordinateRoleId FROM relationship AS r1 LEFT JOIN relationship AS r2 ON r2.supervisorRoleId = r1.subordinateRoleId LEFT JOIN relationship AS r3 ON r3.supervisorRoleId = r2.subordinateRoleId WHERE r1.supervisorRoleId = :roleId" ); query.setParameter( "roleId", roleId ); List<Object[]> result = (List<Object[]>) query.getResultList();
I wind up with results like: 1,1,1 2,2,2 3,3,3
where the first value is repeated. I believe this is because the three fields returned from the query are the same field from different instances (on a self join). I tried changing this to give each return field a different name using "AS r1", etc but this gives a different error that subordinateRoleId field is not found. I know that the rows being returned are correct because if I changed the fields returned from the same instances but that are not the same field, the values being returned are correct (i.e. the instances from which values are returned are correct).
Is there any way to work around this problem?
Regards, Len
|