4

Hibernate QBE(実際にはSpringのHibernateTemplate.findByExample())を使用して、ユーザー名でユーザーのリストを返そうとしています。「既知の良好な」値を使用して検索します(ユーザー名「JOHN.SMITH」はデータベースに存在します)。

残念ながら、結果は返されません。以下は単体テストです。

@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");

}

queryByExample()メソッドは、汎用DAOで定義されています。

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

QBEが機能するために必要な特別な構成はありますか?

4

4 に答える 4

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.

于 2010-08-03T15:15:21.477 に答える
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.

于 2010-08-03T04:53:18.517 に答える
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--

于 2020-07-24T22:53:03.237 に答える
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.

于 2020-07-26T00:41:37.070 に答える