用于对数据库进行复杂查询的 Hibernate Criteria 类

发布于 2024-10-05 16:24:56 字数 303 浏览 1 评论 0原文

我正在开发一个基于 Struts2 Framework 的 Web 应用程序,用于实现 MVC,以及 Hibernate3 用于通过 DAO 访问数据库,以及<用于映射数据库中的关系对象的strong>DTO。在 Hibernate 上下文中,我想知道用于进行复杂查询的 Criteria Classes 的范围,以及是否还有其他选项可以执行此类任务?

谢谢。

I'm developing a web application based on Struts2 Framework for implementing the MVC, and Hibernate3 for accesing the database by means of DAOs, and DTOs for mapping the relational objects in the database. In the Hibernate context, I would like to know the reach of the Criteria Clases for making complex queries, and also if there is another option for doing such tasks?

Thanks.

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

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

发布评论

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

评论(1

宣告ˉ结束 2024-10-12 16:24:56

我所说的“到达”是指与 SQL 相关的复杂查询的领域,这些查询可以通过特定 Criteria 对象的链接方法调用来构造(如所讨论的 此处)。

根据我的经验,依赖于结果过滤、排序、投影、分组、逻辑连接或析取语句或子查询的最常见任务可以通过以预期方式改变 Criteria 对象来执行。

作为替代方案,您可以使用 Session.createSQLQuery 构建原始 SQL 查询,然后 Query.listQuery.executeUpdate 来执行它。例如:

Sesion sess = getSession();
Query mySelect = sess.createSQLQuery("SELECT * from foo");
List<Entity> results = mySelect.list();

Query myUpdate = sess.createSQLQuery("DELETE * from foo where bar = \"boz\"");
int updateCount = myUpdate.executeUpdate();

显然,您会想从这个幼稚的示例中进行扩展,但您已经明白了。

By "reach," I'm assuming you mean the domain of complex, SQL-related queries that can be constructed via chaining method calls through a specific Criteria object (as discussed here).

In my experience, most common tasks that rely on result filtering, ordering, projection, grouping, logical conjunctive or disjunctive statements, or subquerying can be carried out by mutating a Criteria object in the expected ways.

As an alternative, you can use Session.createSQLQuery to construct a raw SQL query, then Query.list or Query.executeUpdate to execute it. For example:

Sesion sess = getSession();
Query mySelect = sess.createSQLQuery("SELECT * from foo");
List<Entity> results = mySelect.list();

Query myUpdate = sess.createSQLQuery("DELETE * from foo where bar = \"boz\"");
int updateCount = myUpdate.executeUpdate();

Obviously, you'll want to extend from this naive example, but you get the idea.

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