Home > Mobile >  What is the effect of parenthesis on the @Formula annotation?
What is the effect of parenthesis on the @Formula annotation?

Time:06-26

When I use Dynamic mapping on a field for an entity like below

@Entity
class Company{
//other fields removed
@Formula("select count(c.id) from Contact c where c.company_id = id")
private int contactCount;
}

I get the following error when I fetch companies from db.

you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use

But when I change my formula to

@Formula("(select count(c.id) from Contact c where c.company_id = id)")
private int contactCount;

everything works correctly. What's effect of paranthesis at the end and beginning of the formula?

CodePudding user response:

As far as I know, in @Formula, JPA use subqueries, so normally you write a formula using field values and doing "math", like @Formula("field A * field B * 100"); since you are using another query, the need of the () wrapping it. Hope I could help.

  • Related