HibernateTemplate findByExample 没有返回结果
我正在尝试使用 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 工作是否需要任何特殊配置?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我来说这纯粹是愚蠢的。
用作示例的类中包含一些整数和布尔值(基元)。由于这些值默认为 0 和 false,因此查询失败。
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.
你必须传递spring配置文件,否则它将如何获取连接和池信息。使用@注释在类声明上方加载spring文件。
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.
您可以通过调用在 Hibernate 的 Example 类中定义的 exceptZeroes() 方法来排除零值。将 hibernate.show_sql 属性添加到 hibernate.cfg.xml 文件将帮助您查看 Hibernate 创建的 SQL 查询中哪些值设置为 0。
https://docs。 jboss.org/hibernate/orm/5.4/javadocs/org/hibernate/criterion/Example.html#excludeZeroes--
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--
将以下内容添加到 hibernate.cfg.xml 文件中:
<属性名称=“hibernate.show_sql”>true
运行您的应用程序。它将显示 Hibernate 生成的 SQL 查询。
通过查看 where 子句,您将看到哪些字段用于过滤。
Netbeans 屏幕截图
Hibernate 示例对象将使用 exceptZeroes() 方法排除零值属性。
您可以使用 exceptProperty() 方法按名称排除某些属性,也可以使用 exceptNone() 方法排除任何内容。
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
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.