1
我試圖使用@OneToOne帳戶和實體實體之間的關係來過濾數據。 我需要做的是搜索具有Account.glcode =「15」的Entry-sCriteria API組合2實體
這裏是我所做的,但它不工作。
Root<?> root = mainQuery.from(Entry.class);
Predicate predicate = builder.conjunction();
Join<Entry,Account> entries = root.join("account");
predicate = builder.and((builder.equal(entries.get("glCode"),"15")));
我的入門級
@Entity
public class Entry {
@Id
private Long id;
.........
@OneToOne
@JoinColumn(name = "account_id", nullable = false)
private Account account;
這是Account類
@Entity
public class Account {
@Id @GeneratedValue
private Long id;
private Long glCode;
誰能告訴我什麼,我做錯了什麼?
感謝
修訂
public PaginationResult getFilteredData(List<FilterConstraint> filters, Object classname) {
try {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<?> mainQuery = builder.createQuery(classname.getClass());
Root<?> root = mainQuery.from(GLJournalEntry.class);
Predicate predicate = builder.conjunction();
Predicate enumPredicate = builder.conjunction();
Predicate today = builder.conjunction();
Predicate on = builder.conjunction();
for (FilterConstraint constraint : filters) {
//IT is hard coded right now for testing purpose but should be customized for the future
if(constraint.getField().getValue().contains("-")){
String[] items = constraint.getField().getValue().split("-");
Join<GLJournalEntry,GLAccount> entries = root.join("glAccount");
predicate = builder.and((builder.equal(entries.get("glCode"), constraint.getValues().getValue())));
entries.getAttribute().toString();
break;
}
switch (constraint.getOperator()) {
case AFTER:
predicate = builder.and(builder.greaterThan(root.get(constraint.getField().getValue()), constraint.getValues().getStartDate()));
break;
case BEFORE:
predicate = builder.and(builder.greaterThan(root.get(constraint.getField().getValue()), constraint.getValues().getStartDate()));
break;
case BETWEEN:
if (constraint.getField().getType() == FieldDataType.DATE) {
predicate = builder.and(builder.between(root.get(constraint.getField().getValue()), constraint.getValues().getStartDate(), constraint.getValues().getEndDate()));
} else if (constraint.getField().getType() == FieldDataType.INTEGER) {
predicate = builder.and(builder.between(root.get(constraint.getField().getValue()), Integer.valueOf(constraint.getValues().getMinValue()), Integer.valueOf(constraint.getValues().getMaxValue())));
} else {
predicate = builder.and(builder.between(root.get(constraint.getField().getValue()), constraint.getValues().getMinValue(), constraint.getValues().getMaxValue()));
}
break;
case EMPTY:
predicate = builder.and(builder.isEmpty(root.get(constraint.getField().getValue())));
break;
case EQUALS:
if (constraint.getField().getType() == FieldDataType.ENUM) {
Object enumV = null;
if (constraint.getValues().getEnumValue().size() > 1) {
List<Predicate> predicates = new ArrayList<>();
for (EnumValue enumValue : constraint.getValues().getEnumValue()) {
for (Field f : classname.getClass().getDeclaredFields()) {
if (f.getName().equals(constraint.getField().getValue())) {
System.out.println("T");
Class<?> clz = f.getType();
Object[] consts = clz.getEnumConstants();
for (int i = 0; i < consts.length - 1; i++) {
if (consts[i].toString().equals(enumValue.getValue())) {
enumV = consts[i];
break;
}
}
}
}
predicates.add(builder.equal(root.get(constraint.getField().getValue()), enumV));
}
enumPredicate = builder.and(builder.or(predicates.toArray(new Predicate[]{})));
break;
}
for (Field f : classname.getClass().getDeclaredFields()) {
if (f.getName().equals(constraint.getField().getValue())) {
System.out.println("T");
Class<?> clz = f.getType();
Object[] consts = clz.getEnumConstants();
for (int i = 0; i < consts.length - 1; i++) {
if (consts[i].toString().equals(constraint.getValues().getEnumValue().get(0).getValue())) {
enumV = consts[i];
break;
}
}
}
}
enumPredicate = builder.equal(root.get(constraint.getField().getValue()), enumV);
break;
}
predicate = builder.and(builder.equal(root.get(constraint.getField().getValue()), constraint.getValues().getValue()));
break;
case LESS_THAN:
predicate = builder.and(builder.lessThan(root.get(constraint.getField().getValue()), constraint.getValues().getValue()));
break;
case MORE_THAN:
predicate = builder.and(builder.greaterThan(root.get(constraint.getField().getValue()), constraint.getValues().getValue()));
break;
case NOT_EMPTY:
predicate = builder.and(builder.isNotEmpty(root.get(constraint.getField().getValue())));
break;
case ON:
on = builder.between(root.get(constraint.getField().getValue()), DateUtils.getFirstSecondOfDate(constraint.getValues().getStartDate()), DateUtils.getLastSecondOfDate(constraint.getValues().getStartDate()));
break;
case STARTS_WITH:
case TODAY:
today = builder.and(builder.between(root.get(constraint.getField().getValue()), DateUtils.getFirstSecondOfDate(new Date()), DateUtils.getLastSecondOfDate(new Date())));
break;
}
}
CriteriaQuery<Long> cq = builder.createQuery(Long.class);
cq.select(builder.count(cq.from(classname.getClass())));
em.createQuery(cq);
cq.where(predicate, enumPredicate, today, on);
Long count = em.createQuery(cq).getSingleResult();
mainQuery.where(predicate, enumPredicate, today, on);
//Count for pagination
TypedQuery<?> q = em.createQuery(mainQuery);
q.setMaxResults(filters.get(0).getCount());
int firstResult = filters.get(0).getPage() * filters.get(0).getCount() - filters.get(0).getCount();
q.setFirstResult(firstResult);
PaginationResult result = new PaginationResult();
result.setData((List<Client>) q.getResultList());
result.setMaxResults(count);
System.out.println(result.getData().size());
return result;
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
}
它拋出一個錯誤:無法解析屬性:的glCode:ge.shemo.model.accounting.glAccount.Entry – Purmarili
你的類必須的getter/setter? – jklee
請發佈您的完整方法。 – jklee