我的数据库中有一个名为 V_ENTITY
的 View ,其中填充了一些条目。在 Eclipse 中使用 SQL 剪贴簿,我可以轻松执行返回所需结果的查询。
但是,当我尝试从 DAO 类中检索条目时,我什么也没得到。我的计数返回 0 作为条目数,而其中有 7 个,我可以通过剪贴簿使用 native SQL 查询获取这些条目。
我为该特定 View 创建了一个单独的实体:
@Entity
@Table(name = "V_ENTITY")
public class ViewEntity {
...
}
我使用 JPA2 CriteriaQuery API 进行动态查询。这是困扰我的计数示例:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<ViewEntity> transactionQuery = cb.createQuery(ViewEntity.class);
Root<ViewEntity> ViewEntityRoot = transactionQuery.from(ViewEntity.class);
Predicate[] predicates = new Predicate[<someSize>]
predicates[<someIndex>] = cb.equal(
root.get("foreignName").as(String.class),
"%" + foreignName + "%"
)
CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
countQuery.select(cb.count(ViewEntityRoot));
countQuery.where(predicates);
TypedQuery<Long> finalQuery = entityManager.createQuery(countQuery);
return finalQuery.getSingleResult().intValue();
任何人都可以帮助我找到我的代码/思维中的缺陷吗?
最佳答案
使用<property name="show_sql">true</property>
属性向我们展示生成的 sql
关于java - 使用 Java 从数据库 View 中未检索到结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19927727/