使用参数过滤 CayenneDataObject getXXXArray() 条目?
我的数据库模型如下:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在卡宴的友好邮件列表上问过类似的问题。您可以查看此处。
首选方法似乎是通过 Java 中的关系和过滤器,除非关系返回非常大的数据。这样做的好处是完整的列表将保存在内存中,并且下次使用关系时,您不需要访问数据库。
答案引用在这里
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