Hibernate weirdness with property names - No Fluff Just Stuff

Hibernate weirdness with property names

Posted by: Pramod Sadalage on October 24, 2008

Consider this Hibernate mapping

@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.

public List findByQReferenceId(Long id) {
     final SimpleExpression matchesId = Property.forName("qReferenceId").eq(id);
    DetachedCriteria criteria = DetachedCriteria.forClass(Movie.class);
    criteria = criteria.add(matchesId);
    List movies = (List) getHibernateTemplate().findByCriteria(criteria);
    return movies;
}

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


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

public List findByQReferenceId(Long id) {
    final SimpleExpression matchesId = Property.forName("QReferenceId").eq(id);
    DetachedCriteria criteria = DetachedCriteria.forClass(Movie.class);
    criteria = criteria.add(matchesId);
    List movies = (List) getHibernateTemplate().findByCriteria(criteria);
    return movies;
}
Pramod Sadalage

About Pramod Sadalage

Pramod Sadalage is the co-author of the 2007 Jolt Productivity Award winning
“Refactoring Databases: Evolutionary Database Development”, a Martin Fowler
signature series book and author of “Recipes for Continuous Database
Integration”. Pramod works as a DBA and developer at ThoughtWorks. He works on
large custom-developed applications that use agile methodologies. While on these
projects, he pioneered the practices and processes of agility in the database.
Pramod writes and speaks about these concepts and practices to help those
interested in using agile practices on databases. He has written and presented
about database administration on XP projects, the adoption of agile processes
with databases, and the impact of agile practices on database administration and
design. Pramod is the owner and founder of the agiledatabases Yahoo! group; he
moderates group discussions and helps group members learn about agile databases.
When he is not working, you can find him spending time with his wife Rupali and
daughter Arula, and trying to improve his running.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »