4

I'm trying to use Hibernate QBE (actually, Spring's HibernateTemplate.findByExample() ) to return a list of users by their username. I use a "known good" value to search on (the username "JOHN.SMITH" does exist in the database).

Unfortunately, I get no results back. Below is the unit test.

@Test
public void testQueryByExample() {

    User qbeUser = new User();
    qbeUser.setUsername("JOHN.SMITH");

    List<User> userList = userDao.queryByExample(qbeUser);
    Assert.notNull(userList);
    Assert.isTrue(userList.size() > 0, "List of returned users must not be 0");

}

The queryByExample() method is defined in a generic DAO:

@SuppressWarnings("unchecked")
public List<T> queryByExample(T obj) {
    return getHibernateTemplate().findByExample(obj);
}

Is there any sort of special configuration needed for QBE to work?

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Jason
  • 3,943
  • 12
  • 64
  • 104

4 Answers4

7

It was pure stupidity on my part.

The classes being used as examples had some ints and booleans (primitives) in them. Since those values default to 0 and false, the queries were failing.

Jason
  • 3,943
  • 12
  • 64
  • 104
  • 5
    Yes, `findByExample()` includes all primitive attributes in its criteria, but ignores all non-primitive attributes whose value is null. The Hibernate `Example` class, however, has more flexible criteria options. See http://docs.jboss.org/hibernate/stable/core/reference/en/html/querycriteria.html#querycriteria-examples. – Derek Mahar Aug 03 '10 at 22:10
  • Amazing comment. I spent a long time looking for the right docs and couldn't find them. – ashes999 Feb 14 '11 at 16:53
0

you have to pass spring configuration file otherwise how would it will get connection and pooling information.. use @ annotation to load spring file above class declaration.

TaherT
  • 1,285
  • 2
  • 22
  • 41
  • The Test class is annotated with @ContextConfiguration(locations = "classpath:applicationContext.xml") The class under test is being successfully injected. My other methods that don't use QBE are fine, but QBE doesn't work. – Jason Aug 03 '10 at 13:44
0

You could exclude zero values by calling the excludeZeroes() method which is defined in the Example class in Hibernate. Adding hibernate.show_sql property to your hibernate.cfg.xml file will help you to see which values are set to 0 in the SQL query that Hibernate creates. https://docs.jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/criterion/Example.html#excludeZeroes--

yozbek
  • 1
0

Add the following to your hibernate.cfg.xml file:
<property name="hibernate.show_sql">true</property>

Run your application. It will display the SQL query that Hibernate generates. By looking at the where clause, you will see what fields are used for filtering. Netbeans screenshot

Criteria c = session.createCriteria(Parking.class);//deprecated since 5.2
Parking p = new Parking();
p.setCity("BROOKLYN");
Example ex = Example.create(p);
ex.excludeZeroes();   //exclude zero-valued properties
c.add(ex);

Hibernate Example object will exclude zero-valued properties with the excludeZeroes() method.

You may exclude certain properties by name with excludeProperty() method, or you may exclude nothing with the excludeNone() method.

yozbek
  • 1