我正在使用 Hibernate 4.3.7/jpa 2,我注意到从 hibernate 4.3 开始 我可以将 Oracle 查询提示嵌入到 hibernate 查询中,抛出 addQueryHint,我尝试使用标准 api 和 hql 来修复它,但不幸的是它对我不起作用!
有什么办法可以运行这个吗?这是我的代码
公共(public) ListgetAll() {
Session session = em.unwrap(org.hibernate.Session.class);
Criteria criteria = session.createCriteria(Person.class).add(Restrictions.like("surname", "H%"));
criteria.addQueryHint("ALL_ROWS");
return criteria.list();
}
任何回复将不胜感激
谢谢
最佳答案
我对 addQueryHint 没有任何运气。根据文档,它应该传递提示,但是当我查看发送的 SQL 时,提示不存在。我注意到此功能的测试使用的是 Oracle 方言,而我们使用的是 SQL Server,所以我认为它可能没有在 SQL Server 中实现,但我发现您在 Oracle 中遇到了问题。
以下是文档中的描述:
Add a DB query hint to the SQL. These differ from JPA's QueryHint, which is specific to the JPA implementation and ignores DB vendor-specific hints. Instead, these are intended solely for the vendor-specific hints, such as Oracle's optimizers. Multiple query hints are supported; the Dialect will determine concatenation and placement.
无论如何,我发现了一个非常糟糕的黑客/解决方法,但我希望看到 addQueryHint 得到修复。
.add(Restrictions.sqlRestriction("1=1 ADD_YOUR_HINT_HERE")))
关于java - 如何向 Hibernate criteria api 查询添加优化器提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27271329/