简体   繁体   中英

How to use @Formula annotation in Teneo/EMF

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?

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 ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM