There are reports that messages of this sort were due to bugs in one or more Hibernate releases. But this is also a legitimate error.
I wasted a lot of time before it hit me. The original code was:
@SuppressWarnings("unchecked") public List<State> getStatesForCountry(String countryID) { final String SQLCOMMAND = "SELECT c " + "FROM " + State.class.getSimpleName() + " c " + " WHERE c.parentCountry = :countryID" + " ORDER BY c.StateName ASC"; Query query = entityManager.createQuery(SQLCOMMAND); query.setParameter("countryID", countryID); List<State> results = query.getResultList(); return results; }
It should have been:
@SuppressWarnings("unchecked") public List<State> getStatesForCountry(String countryID) { final String SQLCOMMAND = "SELECT c " + "FROM " + State.class.getSimpleName() + " c " + " WHERE c.parentCountry = :country" + " ORDER BY c.StateName ASC"; Query query = entityManager.createQuery(SQLCOMMAND); Country country = findCountry(countryID); query.setParameter("country", country); List<State> results = query.getResultList(); return results; }
Where findCountry is simply an entityManager.find(Country.class, countryID);
This is a sneaky one because when the database is laid out, you think in terms of foreign keys. But in ORM, you don’t see those keys directly – they translate to object references. So the original version was attempting to compare an object to the key of the object instead of an instance of the object.
I can only plead distraction. The object model in question had been designed by someone else. Instead of accessor methods, it was using direct field access (which I avoid for a number of reasons). Further aggravating the issue was that the fields were all given names starting with an upper-case letter as though they were independent classes instead of properties.
But even without distractions, it’s not too hard to make this mistake.