Hibernate Sum Based Property

I found out that I can use hibernate to get the sum of the number of objects 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 can only navigate through the database through a graphic object. The problem with having to use the above function is that I have to execute the following psuedo code ...

  • Get an instance of the Envelope entity
  • Skip Envelope i
  • By the result, getEnvelopeTotal sets the Envelope instance property "total" for the result.

What interests me is whether it is possible to use sleep mode so that the "total" property is set using a custom HQL query instead of matching with a simple database column.

ex:

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

Any suggestions?

+3
2

, .

:

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

, (, ), , bean. , id something.id, this.id this.price.

+10

Source: https://habr.com/ru/post/1712056/


All Articles