Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the effect of parenthesis on the @Formula annotation?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading