基于sum的Hibernate属性 [英] Hibernate property based on sum

查看:66
本文介绍了基于sum的Hibernate属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发现我可以使用hibernate来获取使用HQL的许多实体的总和,如下所示:

  public long getEnvelopeTotal(AbstractEnvelope envelope){
String query =从User_Transaction t中选择sum(t.amount);
Long结果=(Long)hibernateUtil.getSession()。createQuery(query).uniqueResult();
返回结果;
}

目前,我的应用程序的其余部分能够通过对象无缝地导航数据库只有图表。不得不使用上述函数的问题是我必须执行以下伪代码...


  1. 获取实体Envelope

  2. 传递Envelope实例i

  3. 根据getEnvelopeTotal的结果将Envelope实例属性total设置为结果。 b $ b

我想知道是否可以通过定制HQL查询设置属性total的方式来使用hibernate,而不是映射到一个简单的数据库列。



=从User_Transaction t中选择总和(t.amount))
私人总长;

有何建议?

解决方案

在阅读这个博客后,阿哈我想通了现在的代码如下所示:

  @Entity(name =Abstract_Envelope)
public abstract class AbstractEnvelope extends AbstractAccount {
$ b $ @ @OneToMany(mappedBy =abstractEnvelope)
private List< UserTransaction> userTransactions;
@Formula(value =从User_Transaction中选择sum(t.amount)t其中t.abstractenvelope_id = id)
私有长整型;

公共列表< UserTransaction> getUserTransactions(){
返回userTransactions;
}

public void setUserTransactions(List< UserTransaction> userTransactions){
this.userTransactions = userTransactions;
}

public Long getTotal(){
return total;
}

public void setTotal(Long total){
this.total = total;


$ / code>

请注意,您必须注解字段而不是方法(以及我不得不),而编写这些查询时,你指的是实际的DB列名称而不是bean属性名称。另外通过引用id而不是something.id,您可以有效地引用this.id或this.price。


I have figured out that I can use hibernate to get the sum of a number of entities using HQL as follows...

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;
}

Currently the rest of my application is able to seamlesly navigate the database via the object graph only. The problem with having to use the function above is that I have to do the following psuedo code...

  1. Get instance of Entity "Envelope"
  2. Pass Envelope instance i
  3. Based on the result of getEnvelopeTotal set the Envelope instance property "total" to the result.

What I am wondering if it is possible to use hibernate in such a way that the property "total" is set via custom HQL query instead of being mapped to a simply database column.

ex:

@SomeMagicAnnotation(query="select sum(t.amount) from User_Transaction t")
private Long total;

Any suggestions?

解决方案

Aha I figured it out after reading this blog post.

The code now looks like this:

@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;
    }
}

Note that you have to annotate the field instead of the method (well I had to) and than when writing these queries you are referring to the actual DB column name not the bean property name. Also by referring to id instead of something.id you are referring to this.id or this.price effectively.

这篇关于基于sum的Hibernate属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆