Hibernate property based on sum

北城以北 提交于 2019-12-06 02:51:29
benstpierre

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!