I have One entity class, its service and repository as follows:
@Entity
@Table(name = "user")
public class User implements Serializable{
@Id
@Column(name = "id", unique = true)
private String userId;
@Column(name = "user_name")
private String userName;
@Column(name = "emp_code")
private String empCode;
// ... other properties
}
Repository
@Repository
public interface UserRepository extends PagingAndSortingRepository<User, String>
{
// .... working
@Query("select u.userName from User u")
Page<User> findAllUserName(Pageable pageable);
//... not working
@Query("select u.userName, u.empCode from User u")
Page<User> findAllUserNameAndEmpCode(Pageable pageable);
}
When I am trying to execute findAllUserName
it works properly. but when using findAllUserNameAndEmpCode
.. it throws following exceptions while starting tomcat:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found ',' near line 1, column 29 [select count(u.userName,u.empCode) from com.entity.User u]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)
... 63 more
I dont know why, and how its converting this query to SELECT count(..)
? What is meaning of expecting CLOSE, found ','
??
Please help.. Thanks
You should specify the count query. The Page return value of your select function needs to know how many results there will be. So it sends a COUNT query that is probably made from your select query and looks like this:
select count(u.userName,u.empCode) from com.entity.User u
which is wrong because COUNT function takes only one parameter. So you should create your custom count query (probably like this):
select count(u.userName) from com.entity.User u
and place it into @Query
annotation:
@Query(
value = "select u.userName, u.empCode from User u",
countQuery = "select count(u.userName) from com.entity.User u"
)
Page<User> findAllUserNameAndEmpCode(Pageable pageable);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With