Hibernate - javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter

March 31, 2016

Problem:

javax.persistence.PersistenceException: org.hibernate.PropertyAccessException: could not get a field value by reflection getter
<![CDATA[ public BigDecimal getMinPrice(Product product){ logger.debug(“Entering getMinPrice. productId="+product.getId()); Session session = this.sessionFactory.openSession(); Criteria criteria = session.createCriteria(ShopProductPriceHistory.class); criteria.add(Restrictions.eq(“product”, product.getId())); criteria.setProjection(Projections.min(“price”)); return (BigDecimal) criteria.uniqueResult(); } ]]>

Solution:

Check that the criteria is being done properly via join (Hibernate “Alias”).
<![CDATA[ public BigDecimal getMaxPrice(Product product){ logger.debug(“Entering getMaxPrice. productId="+product.getId()); Session session = this.sessionFactory.openSession(); Criteria criteria = session.createCriteria(ShopProductPriceHistory.class); criteria.createAlias(“product”, “product”).add(Restrictions.eq(“product.id”, product.getId())); criteria.setProjection(Projections.max(“price”)); return (BigDecimal) criteria.uniqueResult(); } ]]>
SyntaxHighlighter.highlight();