什么是 org.hibernate.criterion.Example class?

what is org.hibernate.criterion.Example class?

我是休眠 ORM 的初学者。我找到了一个 class Example(org.hibernate.criterion.Example) 。我搜索了很多关于相同 class 的内容,但我找不到它的实际用途或任何好的例子。 Hibernate 文档没有为我提供足够的信息来理解这个 class.

如果有人能给我一个最好的例子并解释这个例子的用途就太好了class

提前致谢

该示例构建 where 块。

要找到每个 18 岁的用户,应该是:

User u = User();
u.setAge(18);
Collection<User> users = session.createCriteria(User.class).add(Example.create(u)).list();
// SELECT * FROM User WHERE age=18

集合 users 中只有 18 岁的用户。

提示: 该示例内省每个不是 null 的字段。这意味着如果主键是 int,默认情况下它是 0 并添加到 WHERE age=18 AND id=0 的 where 块中。使 pk 成为 Integer 以正确使用它。