Issue
My class
@Repository
public class UserRepositoryImp implements UserRepository {
private EntityManager entityManager;
@Autowired
public UserRepositoryImp(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public Integer getCountByEmail(String email) {
Session session = entityManager.unwrap(Session.class);
TypedQuery<User> query = session.createQuery("SELECT count(e) FROM User e WHERE e.email=:email", User.class);
query.setParameter("email", email);
return query.executeUpdate();
}
My Postman error
"Type specified for TypedQuery [com.TestMobileAppService.TestMobileAppService.Domain.User] is incompatible with query return type [class java.lang.Long]; nested exception is java.lang.IllegalArgumentException: Type specified for TypedQuery [com.TestMobileAppService.TestMobileAppService.Domain.User] is incompatible with query return type [class java.lang.Long]"
Please help me what is it problem ???
Solution
Your query in variable `MY_SQL_COUNT_EMAIL` returns a count instead of User object, Change the data type of variable either to Long or Integer, something like given below
@Override
public Integer getCountByEmail(String email) {
Session session = entityManager.unwrap(Session.class);
TypedQuery<Integer> query = session.createQuery(MY_SQL_COUNT_EMAIL,Class.forName("java.lang.Integer")); //Try this
TypedQuery<Integer> query = session.createQuery(MY_SQL_COUNT_EMAIL); // OR Try this
query.setParameter("email", email);
return query.executeUpdate();
}
Answered By - Ahmad Qureshi
Answer Checked By - Candace Johnson (JavaFixing Volunteer)