I am using ireport-4.5.0,spring3.0.5RELEASE,jpa2. I have an entity class named as User and having the attributes as follow
userId,
userName,
password.I
Designed the jrxml by giving the fields as these three attributes names.If i give the jpa Query as From USer then it is working fine and giving the result. But i want the distinct values of userName(userName column allows duplicate values also)so i have given the jpaQuery as
SELECT userId,DISTINCT(userName),password FROM User
Then i am not getting the result.What is the problem with the query.
Hi here i am giving the code i am using
public User getUsers() throws Exception{
Query uQuery = entityManager.createQuery("SELECT u.userId,u.userName,u.password FROM User u GROUP BY u.userId,u.userName,u.password");
List <User>listOfUsers = uQuery.getResultList();
if (listOfUsers == null) {
throw new ResourceNotFound();
}
for (Iterator iterator = listOfUsers.iterator(); iterator.hasNext();) {
User userList = (User)iterator.next();
}
return userList;
Here i am getting the ClassCastException:java.lang.String cannot be cast to User.It is showing this exception in the for loop statement.I am new to JPA.Can you please explain how to iterate that list of objects.
You cannot put DISTINCT on a single column in JPQL or SQL.
You most likely need to use a group by,
SELECT u.userId,u.userName,u.password FROM User u group by u.userId, u.userName, u.password