JPA命名查询与条件API? [英] JPA Named Queries vs Criteria API?

查看:83
本文介绍了JPA命名查询与条件API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准API NamedQuery 之间进行决策时是否有启发式/最佳实践/规则集?

Is there a heuristic/best practice/ruleset for a decision between the Criteria API and NamedQuery?

到目前为止我的想法:
命名查询通常更具可读性.条件查询更加灵活.
两者都是预编译的.我倾向于尽可能长时间地使用命名查询,然后更改为条件.

但也许 通过使用条件API灵活化"查询的冲动是否暗示了欠佳的设计(即关注点分离)?

My thoughts so far :
Named queries are generally more readable. Criteria queries are more flexible.
Both are precompiled. I tend to rely on using named queries as long as possible, then changing to criteria.

But maybe the urge to "flexify" the query by using the criteria API is a hint to suboptimal design (i.e. separation of concerns)?

谢谢

推荐答案

命名查询更为理想(它们被解析/准备一次).条件查询是动态的(尽管某些JPA提供程序(例如EclipseLink维护条件准备高速缓存),它们不是预先编译的).

Named queries are more optimal (they are parsed/prepared once). Criteria queries are dynamic, (they are not precompiled, although some JPA providers such as EclipseLink maintain a criteria prepare cache).

我只会将条件用于动态查询.

I would use criteria only for dynamic queries.

这篇关于JPA命名查询与条件API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆