【发布时间】:2019-02-09 00:25:42
【问题描述】:
我有 2 个具有一对多关系的类。 客户(类)有很多交易(类)
public class Customer {
@Id
private Long clientId;
private String name;
@OneToMany
private List<Transactions> transactions;
}
public class Transactions {
@JoinColumn(name = "clientId")
private Transactions transactions;
private int statusType;
private String amount;
}
int dynamicValue = 1003;
CriteriaQuery<Customer> criteriaQuery = getBuilder().createQuery(Customer.class);
Root<Customer> customersRoot = criteriaQuery.from(Customer.class);
Join<Customer, Transactions> transactions = customersRoot.join("transactions");
TypedQuery<Customer> query = em.createQuery(criteriaQuery.select(customerRoot).where(getBuilder().equal(transactions.get("statusType"), dynamicValue)));
List<Customer> customerList = (List<Customer>) query.getResultList();
我有 2 个来自数据库的数据: 客户表
ClientId | Name |
1 | James |
2 | Eli |
交易表:
ClientId | Status Type| Amount| TransactionId |
1 | 1002 | 100 | 1 |
1 | 1003 | 200 | 2 |
我需要在上面进行查询以接受多个参数(动态)。这些参数将来自客户的属性,例如名称,一些参数将来自 Transactions 类。但是,当我尝试执行上面的代码时,它总是在我的数据库中获得第一条记录(1002),这是不正确的。
请给我一点。 问题:
- 如何在标准构建器中实现多个动态参数?
- 我的查询有什么问题,为什么总是获得第一条记录?
【问题讨论】:
-
如果你用表格形式表示数据就会很容易理解。
标签: java jpa persistence criteria-api