Calculate procedures or queries in hibernate in my entitys

Viewed 70

I have a table called "Endoso", this table has the properties of "id", "numEndoso","endosos_por_dia". The "numEndoso" is the id of the envio(id of table EnvioRemesa), and the "endosos_por_dia" property is a calculated property as I put below.

select count(*)
from documento d
inner join remesa r
on r.id = d.remesa_id
inner join envio_remesa er
on r.id = er.remesa_id
where r.id=?

What I want and can't do, is that for example when I calculate endosos_por_dia if 8 documents turn out, create 8 different records in my database, increasing from 1 to 8.And how could I put my calculated property query on my entity Endoso and it will be reflected in the database? How can I do this in hibernate?

1 Answers

You can use a Formula:

@Formula("(select count(*) ... where r.id=numEndoso)")
public int getEndososPorDia() {
  return endososPorDia;
}

Note that numEndoso in the SQL fragment refers to the ID of your entity, you might need to adapt it because I assumed that it was the name of the column.

This will make the endososPorDia field read-only as far as Hibernate is concerned, you will need to modify the underlying data (add new documents) and then refresh the entity to update the value

Related