This is how my code looks
ublic Users getUser(Users users){
Users usersResult = null;
StringBuilder sql = new StringBuilder();
try {
//sql.append("SELECT u from Users u where 1=1");
sql.append("SELECT u from Users u where 1=1");
if (users.getUserId() != null) {
sql.append(" AND u.userId ='" + users.getUserId() + "'");
}
if (users.getCustomer() != null) {
if (users.getCustomer().getCustomerId() != null) {
sql.append(" AND u.customer.customerId ='" + users.getCustomer().getCustomerId() + "'");
}
if (users.getCustomer().getId() != null) {
sql.append(" AND u.customer.id ='" + users.getCustomer().getId() + "'");
}
}
TypedQuery<Users> q = em.createQuery(sql.toString(), Users.class);
usersResult = q.getSingleResult();
} catch (Exception e) {
logger.error("Getting Exception getUser "+e.getMessage());
return null;
}
return usersResult;
}
I am getting the following exception
ERROR [co.ic.bi.re.UserRepository] (main) Getting Exception getUser org.hibernate.query.sqm.InterpretationException: Error interpreting query [SELECT u from Users u where 1=1 AND u.userId ='071e4b35-7a7a-4f6a-b9fb-c7eecac65c45' AND u.customer.id ='19']; this may indicate a semantic (user query) problem or a bug in the parser [SELECT u from Users u where 1=1 AND u.userId ='071e4b35-7a7a-4f6a-b9fb-c7eecac65c45' AND u.customer.id='19'] If I remove the call of u.customer.id then the query works. I checked the sql in db and looks ok, but my test case fails, so seems hibernate ORM doesn't like it.
This is how the user entity lazily loads the customer object.
@JoinColumn(name = "CUSTOMER_ID", referencedColumnName = "ID", nullable =false)
@ManyToOne(fetch = FetchType.LAZY)
private Customer customer;