我已经发现我可以使用hibernate来获取使用HQL的许多实体的总和,如下所示......
public Long getEnvelopeTotal(AbstractEnvelope envelope) {
String query = "select sum(t.amount) from User_Transaction t";
Long result = (Long) hibernateUtil.getSession().createQuery(query).uniqueResult();
return result;
}
Run Code Online (Sandbox Code Playgroud)
目前,我的应用程序的其余部分只能通过对象图无缝地导航数据库.必须使用上述函数的问题是我必须执行以下伪代码...
我想知道是否有可能以这样的方式使用hibernate,通过自定义HQL查询设置属性"total",而不是映射到简单的数据库列.
例如:
@SomeMagicAnnotation(query="select sum(t.amount) from User_Transaction t")
private Long total;
Run Code Online (Sandbox Code Playgroud)
有什么建议?
ben*_*rre 10
啊哈,我在阅读这篇博文后想通了.
代码现在看起来像这样:
@Entity(name = "Abstract_Envelope")
public abstract class AbstractEnvelope extends AbstractAccount {
@OneToMany(mappedBy = "abstractEnvelope")
private List<UserTransaction> userTransactions;
@Formula(value = "select sum(t.amount) from User_Transaction t where t.abstractenvelope_id = id")
private Long total;
public List<UserTransaction> getUserTransactions() {
return userTransactions;
}
public void setUserTransactions(List<UserTransaction> userTransactions) {
this.userTransactions = userTransactions;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,您必须注释字段而不是方法(我必须这样做),而且在编写这些查询时,您指的是实际的DB列名称而不是bean属性名称.另外通过引用id而不是something.id,你有效地引用this.id或this.price.
| 归档时间: |
|
| 查看次数: |
8423 次 |
| 最近记录: |