使用参数过滤 CayenneDataObject getXXXArray() 条目?

发布于 2024-10-03 15:19:32 字数 825 浏览 4 评论 0原文

我的数据库模型如下:

A.id (1 : n) B.ad_id

因此,在辣椒中,对于对象 A a 我可以执行 a.getBArray() ,它会返回给定 A 条目中 B 的所有条目。但我想根据属性 active = 1 对此列表进行过滤。

显然我可以将 Expression.fromString("active = 1")SelectQuery 一起使用,但对于这种方法,我找不到如何关联我想要的 A 实例运行此查询。

另一种方法是从 a.getBArray() 检索所有条目,并在代码中进行过滤,仅搜索那些具有 active == true 的条目,恕我直言,这种方法效率低下。

建议大多受到赞赏。

谢谢你, 格言。

-- 编辑:

我当前的解决方案是(对象名称已分别替换为 a 和 b):

long aId = DataObjectUtils.longPKForObject(db_a_instance);
String bSQL = "select * from b where active = 1 and a_id = " + aId;
SQLTemplate bQuery = new SQLTemplate(B.class, bSQL);
List<B> dbBs = context.performQuery(bQuery);

我问是否有更好、更优雅的解决方案?

谢谢。

My DB model is as following:

A.id (1 : n) B.ad_id

So in cayenne for object A a I can do a.getBArray() which returns me all the entries from B from this given A entry. Yet I would like to filter on this list, based on the property active = 1.

Obviously I can use Expression.fromString("active = 1") with SelectQuery, but for this approach I can't find how I associate the A instance under which I want to run this query on.

A different approach is to retrieve all entries from a.getBArray() and filter in code searching only those that have active == true, this approach is IMHO inefficient.

Recommendations are mostly appreciated.

Thank you,
Maxim.

-- EDIT:

My current solution to is (object names have been replaced with a & b respectively):

long aId = DataObjectUtils.longPKForObject(db_a_instance);
String bSQL = "select * from b where active = 1 and a_id = " + aId;
SQLTemplate bQuery = new SQLTemplate(B.class, bSQL);
List<B> dbBs = context.performQuery(bQuery);

and I'm asking if there is a better, more elegent solution?

Thanks.

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

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

发布评论

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

评论(1

悸初 2024-10-10 15:19:32

我在卡宴的友好邮件列表上问过类似的问题。您可以查看此处

首选方法似乎是通过 Java 中的关系和过滤器,除非关系返回非常大的数据。这样做的好处是完整的列表将保存在内存中,并且下次使用关系时,您不需要访问数据库。

答案引用在这里

两者都需要访问数据库。

  1. (关系方法中的遍历)需要访问一次数据库以从数据库中对组进行故障排除,但随后它将存储在内存中。

  2. (使用过滤方法的查询)每次都需要访问数据库,因此从长远来看,即使返回的匹配项较少,速度也可能会变慢。

如果这种事情只发生一次,而你真的
关心性能(并且可能有很多组),我会
选择#2,否则选择#1。你也可以稍微优化#1,这样你
不必每次都迭代检查。

来自:迈克尔·金特里

I've asked similar question on Cayenne's friendly mailing list. You can see here.

It seemed to be the preferred approach is to go via relationship and filter in Java unless the relationship returns very large data. The benefit of doing so that the full list will be in memory and next time when you use relationship, you don't need to make a trip to DB.

The answer is quoted here

Both require a trip to the DB.

  1. (the traversing in relationship approach) requires a trip one time to the DB to fault the groups from the DB, but then it'll be in memory.

  2. (the query with filter approach) requires a trip to the DB every time, so that could be slower in the long run even though it returns fewer matches.

If this is something that only happens once and you are REALLY
concerned about performance (and possibly have a LOT of groups), I'd
go with #2, otherwise #1. You can optimize #1 a bit, too, so you
don't have to iterate each time to check.

via: Michael Gentry

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