I have 2 models with one to many relationships

Policy to Documents

When i tried to execute this sql

String sql = select doc from Document doc where doc.policy =:policyId and doc.templateId=112 and doc.status=3
Query query = entityManager.createQuery(sql);
query.setParameter("policyId", policy.getPolicyId());
List result = query.getResultList();

I got the following error

15:55:14,315 ERROR [TxPolicy] javax.ejb.EJBTransactionRolledbackException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of com.ace.project.model.Policy.policyId

After googling, i found the problem was at the SQL above i set policyId parameter with policyId (policyId is PK of Policy object) instead of Policy Object.

The correct SQL should be

String sql = select doc from Document doc where doc.policy =:policyId and doc.templateId=112 and doc.status=3
Query query = entityManager.createQuery(sql);
query.setParameter("policyId", policy); //THIS LINE IS THE FIXED
List result = query.getResultList();

Leave a Reply