Я пытаюсь получить данные из таблицы mysql с некоторыми условиями и упорядочением с использованием JPA. Мой код работает отлично без порядка, если я включил порядок, я получаю ноль записей (это не ожидалось).
Я получаю сгенерированный запрос из консоли и запускаю в mysql cmd. Я получаю результат, но не работаю через JPA
Пожалуйста, помогите мне
USER TABLE
--------------------------------------------------
NAME | AGE | ROLE | ROLE_CD | CITY_CODE
-------------------------------------------------
name1 | 24 | developer | SR_DEV | ch1 |
name2 | 25 | tester | SR_QAE | ch2 |
name3 | 26 | developer | DEV | ch1 |
name4 | 27 | tester | QAE | ch4 |
ADDRESS TABLE
--------------------------------
STATE | CITY | CITY_CODE
--------------------------------
state1 | city1 | ch1 |
state2 | city2 | ch2 |
state3 | city3 | ch3 |
state4 | city4 | ch4 |
public class User {
@Column(name = "NAME")
private String name;
@Column(name = "AGE")
private String age;
@Column(name = "ROLE")
private String role;
@Column(name = "ROLE_CD")
private String roleCd;
@Column(name = "CITY_CODE")
private String cityCode;
@OneToOne
@JoinColumn(name = "cityCode", referencedColumnName = "cityCode",
insertable = false, updatable = false,
foreignKey = @javax.persistence.ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
private Address address;
}
public class Address {
@Column(name = "CITY_CODE")
private String cityCode;
@Column(name = "CITY")
private String city;
@Column(name = "STATE")
private String state;
}
public class OrderByField implements Order {
private Expression<?> expression;
@Override
public Order reverse() {
return null;
}
@Override
public boolean isAscending() {
return true;
}
@Override
public Expression<?> getExpression() {
return expression;
}
public OrderByField(CriteriaBuilder cb, List<Expression<?>> list) {
this.expression = cb.function("FIELD", String.class, list.toArray(new Expression<?>[list.size()]));
}
}
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<User> criteriaQuery = builder.createQuery(User.class);
Root<User> userData = criteriaQuery.from(User.class);
Join<User, Address> joinAddress = userData.join("address");
//Order By field ROLE_CODE
List<Expression<?>> roleList = new ArrayList<>();
roleList.add(userData.get("roleCd"));
roleList.add(builder.parameter(String.class, "param1"));
roleList.add(builder.parameter(String.class, "param2"));
OrderByField order = new OrderByField(builder, roleList);//created custom class for ordeBy field
//WHERE CLUASE city like city1 and role like developer
List<Predicate> predicates = new ArrayList<>();
predicates.add(builder.like(joinAddress.get("city"), "%" + "city1" + "%"));
predicates.add(builder.like(userData.get("role"), "%" + "developer" + "%"));
criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
criteriaQuery.orderBy(order);
TypedQuery<User> query = em.createQuery(criteriaQuery);
//ORDER BY FIELD PARAMS
query.setParameter("param1", "SR_DEV");
query.setParameter("param2", "DEV");
//THIS FOR LIMIT THE RECORD
if (pageNumber != null && pageSize != null) {
query.setFirstResult(pageNumber * pageSize);
query.setMaxResults(pageSize);
}
return query.getResultList();