How to use @Formula annotation in Teneo/EMF

119 views Asked by At

I am using EMF and Teneo. I have an Employee class with a derived attribute. I also defined a Hibernate @Formula annotation for it as follows:

    * @model 
    *   default="0" transient="true" changeable="false" 
    *   derived="true" ordered="false"
    *   annotation="teneo.jpa 
    *   appinfo=' @Formula(\"select count(*) from Employee o where o.parent_e_id =e_id\")'"

I was expecting the query to fire whenever I load the Employee object by a call to .list().

However, the query to calculate the derived attribute never fires (checked the mysql log).

Any ideas on what I am doing wrong or any pointers on where to look?

1

There are 1 answers

0
user2465039 On

I am answering to my own question since I have found the solution and someone else might need it

The attribute has to be defined as follows:

    * @model default="0" derived="true" ordered="false"
    *        annotation="teneo.jpa 
             appinfo=' @Formula(\"(select count(*)
                       from Employee o where o.parent_e_id=e_id)\")'"

In other words, the attribute

  • must not be transient (false), for otherwise it is completely ignored when teneo generates the mapping (this is why the annotation was ignored)
  • must be changeable (so that the value of the attribute can be set)

Also the SQL statement should be in brackets since hibernate will generate a statement such as

    select field1, field2, (select count (*) from Employe o where    
                        o.parent_e_id_e_id) as fieldname from table1 ...