JPA 计数查询以按查询计算组的总结果行
JPA count query to count total result rows of a group by query
如何在 JPQL 中获取 SQL 查询下面的总结果行:
select
count(usertransa0_.id) as col_0_0_
from
user_transaction usertransa0_ cross
join
merchant_master merchantma1_
where
usertransa0_.merchant_master=merchantma1_.id
and usertransa0_.status=0
and (
usertransa0_.transaction_date between '2015-02-19' and '2015-02-20'
)
group by
merchantma1_.full_name;
我试过:
SELECT COUNT(ut)
FROM UserTransaction ut
WHERE ut.status = :stus AND ut.transactionDate between :frm AND :toDt"
GROUP BY ut.merchantMaster.fullName
但是它returns只计算第一行,如何计算总行数?
编辑:
我的实体看起来像:
public class UserTransaction extends BaseEntity {
@OneToOne(fetch = FetchType.LAZY)
private UserInfo info;
@OneToOne(fetch = FetchType.LAZY)
private MerchantMaster merchantMaster;
@OneToOne(fetch = FetchType.LAZY)
private PaymentMaster paymentMaster;
@Column(name = "AMOUNT")
private BigDecimal amount;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "TRANSACTION_DATE")
private Date transactionDate;
@Column(name = "STATUS")
private Integer status;
private String userTransactionID;
@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
private UserTransactionRequest trRequest;
@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
private UserTransactionResponse trResponse;
//getters & setters
}
您的查询将 return 一个 Long 数组。每个 Long 是给定 MerchantMaster 名称的 UserTransaction
的计数。
如果您在 NamedQuery 中有您的查询,您可以使用(如果没有,您可以使用 creatyQuery()
方法并使用您的 JPQL 查询字符串):
TypedQuery<Long> query = em.createNamedQuery("UserTransaction.MyQuery", Long.class);
/* The list have the count of User Transaction grouped by Merchant name */
List<Long> userTransactionsPerMerchantName = query.getResultList();
您可能还需要商家名称,您可以将商家名称添加到您的查询中:
SELECT COUNT(ut), ut.merchantMaster.fullName
FROM UserTransaction ut
WHERE ut.status = :stus AND ut.transactionDate between :frm AND :toDt"
GROUP BY ut.merchantMaster.fullName
现在,查询将 return 一个对象数组,第一个对象是计数,第二个是名称。您还需要修改代码才能得到结果:
TypedQuery<Object[]> query = em.createNamedQuery("UserTransaction.MyQuery", Object[].class);
List<Object []> results = query.getResultList();
for (Object [] row : results) {
Long count = (Long) row[0];
String name = (String) row[1];
}
如何在 JPQL 中获取 SQL 查询下面的总结果行:
select
count(usertransa0_.id) as col_0_0_
from
user_transaction usertransa0_ cross
join
merchant_master merchantma1_
where
usertransa0_.merchant_master=merchantma1_.id
and usertransa0_.status=0
and (
usertransa0_.transaction_date between '2015-02-19' and '2015-02-20'
)
group by
merchantma1_.full_name;
我试过:
SELECT COUNT(ut)
FROM UserTransaction ut
WHERE ut.status = :stus AND ut.transactionDate between :frm AND :toDt"
GROUP BY ut.merchantMaster.fullName
但是它returns只计算第一行,如何计算总行数?
编辑:
我的实体看起来像:
public class UserTransaction extends BaseEntity {
@OneToOne(fetch = FetchType.LAZY)
private UserInfo info;
@OneToOne(fetch = FetchType.LAZY)
private MerchantMaster merchantMaster;
@OneToOne(fetch = FetchType.LAZY)
private PaymentMaster paymentMaster;
@Column(name = "AMOUNT")
private BigDecimal amount;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "TRANSACTION_DATE")
private Date transactionDate;
@Column(name = "STATUS")
private Integer status;
private String userTransactionID;
@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
private UserTransactionRequest trRequest;
@OneToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
private UserTransactionResponse trResponse;
//getters & setters
}
您的查询将 return 一个 Long 数组。每个 Long 是给定 MerchantMaster 名称的 UserTransaction
的计数。
如果您在 NamedQuery 中有您的查询,您可以使用(如果没有,您可以使用 creatyQuery()
方法并使用您的 JPQL 查询字符串):
TypedQuery<Long> query = em.createNamedQuery("UserTransaction.MyQuery", Long.class);
/* The list have the count of User Transaction grouped by Merchant name */
List<Long> userTransactionsPerMerchantName = query.getResultList();
您可能还需要商家名称,您可以将商家名称添加到您的查询中:
SELECT COUNT(ut), ut.merchantMaster.fullName
FROM UserTransaction ut
WHERE ut.status = :stus AND ut.transactionDate between :frm AND :toDt"
GROUP BY ut.merchantMaster.fullName
现在,查询将 return 一个对象数组,第一个对象是计数,第二个是名称。您还需要修改代码才能得到结果:
TypedQuery<Object[]> query = em.createNamedQuery("UserTransaction.MyQuery", Object[].class);
List<Object []> results = query.getResultList();
for (Object [] row : results) {
Long count = (Long) row[0];
String name = (String) row[1];
}