Passionate about data

Data and its implications on software design and development.

Hibernate Weirdness With Property Names

Consider this Hibernate mapping

1
2
3
4
@Column(name = "qReferenceId")
public Long getQReferenceId() {
return qReferenceId;
}

Where qReferenceId is data provided to our application via a external reference, we do not have a QReference Object or Table for FK references.When trying to query this object using DetachedQuery, this Simple expression was used.

1
2
3
4
5
6
7
public List<Movie> findByQReferenceId(Long id) {
     final SimpleExpression matchesId = Property.forName("qReferenceId").eq(id);
    DetachedCriteria criteria = DetachedCriteria.forClass(Movie.class);
    criteria = criteria.add(matchesId);
    List<Movie> movies = (List<Movie>) getHibernateTemplate().findByCriteria(criteria);
    return movies;
}

When running this method, I kept getting errors shown below.

1
2
3
4
could not resolve property: qReferenceId of: com.example.Movie; 
nested exception is org.hibernate.QueryException: could not resolve property: qReferenceId of: com.example.Movie
....
....

I thought I had a spelling mistake on the property name and also tried many other combinations. Finally I said why not use “QReferenceId” and suddenly everything was hunky dory, this kind of weirdness does not happen when working with property names that do not have consecutive double uppercase characters in the getter/setter for the property. very interesting. So this method worked

1
2
3
4
5
6
7
public List<Movie> findByQReferenceId(Long id) {
    final SimpleExpression matchesId = Property.forName("QReferenceId").eq(id);
    DetachedCriteria criteria = DetachedCriteria.forClass(Movie.class);
    criteria = criteria.add(matchesId);
    List<Movie> movies = (List<Movie>) getHibernateTemplate().findByCriteria(criteria);
    return movies;
}