Hibernate:示例查询相当于关联条件查询

发布于 2024-12-17 08:17:29 字数 706 浏览 1 评论 0原文

我想根据关联相关的对象的值在我的数据源中搜索所有对象实例。数据模型可以简化为:A类型的对象保存B类型的对象列表。目标是找到A的所有实例,其中A包含B,使得B的属性值为X。

我已经可以成功实现使用 Criteria 查询,如下所示:

  List<A> results = session.createCriteria(A.class)
    .createCriteria("listOfBs")
    .add(Restrictions.eq("propertyInB", x))
    .list();

这是一种简化,并且 B 的多个属性将适用 - 搜索功能对于用户填充的过滤器是必需的。

我想用示例查询替换这种方法 - 我只需创建一个具有所需参数的对象图。我尝试遵循 Hibernate 文档失败了,并在 this 中进行了描述问题

我认为以一种有效的方式展示我试图实现的目标可能会有所帮助,然后寻求等效的方法 - 这就是我重新提出这个问题的原因。

简而言之,我的问题是:如何在 Hibernate 中将上述条件查询实现为示例查询?我正在使用 Hibernate 3.6.6。

谢谢!

I'd like to search my data source for all object instances based on the values of an object related by association. The data model can be simplified to: object of type A holds a list of objects of type B. The goal is to find all instances of A where A contains a B such that B has a property value of X.

I can already successfully achieve this using Criteria queries as follows:

  List<A> results = session.createCriteria(A.class)
    .createCriteria("listOfBs")
    .add(Restrictions.eq("propertyInB", x))
    .list();

This is a simplification, and multiple properties of B will apply - the search functionality is necessary for a user populated filter.

I would like to replace this approach with query by example - where I'd simply create an object graph with the desirable parameters. My attempt in following the Hibernate docs failed, and is described in this question.

I thought that it might be helpful to demonstrate what I'm trying to achieve in a manner that works, and then seek equivalents - that's why I'm re-asking the question.

In short, my question is: How would you implement the above Criteria Query as a Query by Example in Hibernate? I'm using Hibernate 3.6.6.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠北森屋 2024-12-24 08:17:29

假设您想做类似的事情:

Select a.* , b* 
from a join b on a.id = b.id 
where a.property1 = "wwww"
and a.property2="xxxx"
and b.property1="yyyy"
and b.property2="zzzz"

使用 Query by Example(QBE) 实现上述查询:

/***Initialize an instance of Class A with the properties that you want to match***/
A instanceA = new A();
instanceA.setProperty1("wwww");
instanceA.setProperty2("xxxx"); 
Example exampleA = Example.create(instanceA);

/***Do the same for the Class B**/
B instanceB = new B();
instanceB.setProperty1("yyyy");
instanceB.setProperty2("zzzz"); 
Example exampleB = Example.create(instanceB);

/**Create and execute the QBE***/
List<A> results = session.createCriteria(A.class)
    .add(exampleA)
    .createCriteria("b",CriteriaSpecification.LEFT_JOIN) // b is the property of Class A
    .add(exampleB)
    .list();

结果已经是 fetch-joined ,这意味着 A 中的集合实例 B 已经完全初始化。

Suppose you want to do something like :

Select a.* , b* 
from a join b on a.id = b.id 
where a.property1 = "wwww"
and a.property2="xxxx"
and b.property1="yyyy"
and b.property2="zzzz"

To implement the above query using Query by Example(QBE):

/***Initialize an instance of Class A with the properties that you want to match***/
A instanceA = new A();
instanceA.setProperty1("wwww");
instanceA.setProperty2("xxxx"); 
Example exampleA = Example.create(instanceA);

/***Do the same for the Class B**/
B instanceB = new B();
instanceB.setProperty1("yyyy");
instanceB.setProperty2("zzzz"); 
Example exampleB = Example.create(instanceB);

/**Create and execute the QBE***/
List<A> results = session.createCriteria(A.class)
    .add(exampleA)
    .createCriteria("b",CriteriaSpecification.LEFT_JOIN) // b is the property of Class A
    .add(exampleB)
    .list();

The result is already fetch-joined , which means the collection instance B in the A is already fully initialized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文