本文介绍了以位AND(&)为标准的JPA @NamedQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道如何使用按位AND(&)作为JPA NamedQuery的标准,而不必使用@NamedNativeQuery?

does anyone know how to use bitwise AND (&) as criteria for a JPA NamedQuery without having to use a @NamedNativeQuery?

我将状态位存储在一个字段中.

I'm storing status bits in a field.

我将以下定义用于视图的实体:

I'm using the following defs for an entity of a view:

@NamedQueries({
    @NamedQuery(name="ViewProductsList.findAll", query="SELECT v FROM ViewProductsList v"),
    @NamedQuery(name="ViewProductsList.findFeatured", query="SELECT v FROM ViewProductsList v WHERE v.statusId & 16"),
    @NamedQuery(name="ViewProductsList.findBySubcategory", query="SELECT v ViewProductsList v WHERE v.subcatId IN (:subcatIds)"),
})

第一个可以正常工作,但是第二个不喜欢该表达式,我得到以下信息:

The first one works fine, but the second does not like the expression, I get the following:

18:02:13,528 DEBUG [DataNucleus.Query] - JPQL Single-String with "SELECT v FROM ViewProductsList v WHERE v.statusId & 16"
18:02:13,553 DEBUG [DataNucleus.Query] - JPQL Query : Compiling "SELECT v FROM ViewProductsList v WHERE v.statusId & 16"
>>ERROR: 'Portion of expression could not be parsed: & 16'

在生产中使用dev MySQL数据库和Google Cloud SQL,因此它必须在两个版本上都可以使用...

Using dev MySQL database and Google Cloud SQL in production, so it would have to work on both versions...

有什么想法吗?

更新的问题:

按如下方式使用定义的@NamedNativeQuery时:

When using a defined @NamedNativeQuery as follows:

@NamedNativeQuery(name = "ViewProductsList.findFeatured", query = "SELECT * FROM view_products_list v")

并按如下方式调用它:

Query query=em.createNativeQuery("ViewProductsList.findFeatured", ViewProductsList.class);
List<ViewProductsList> list = query.getResultList();

我收到以下错误:

java.lang.IllegalStateException: You cannot invoke getResultList/getSingleResult when the Query is an UPDATE/DELETE
    at org.datanucleus.api.jpa.JPAQuery.getResultList(JPAQuery.java:161)
    at cultivartehidroponia.ProductsServlet.doGet(ProductsServlet.java:81) ...

我尝试了许多createNativeQuery示例的变体,但均未成功.有人知道使用数据核的本机查询示例吗?

I have tried many variations of createNativeQuery examples with no success. Does anybody know of working native query examples using datanucleus?

再次,使用带有createNamedQuery的普通@NamedQuery可以正常工作!

Again, using a normal @NamedQuery with createNamedQuery works fine!

提前谢谢!

推荐答案

Bitwise AND不属于JPQL.但是...

Bitwise AND is not part of JPQL. But...

可以使用2条规则:

  1. 除以2 ^х会将位右移x(例如1011010/1000 = 1011)
  2. 如果右位为1(例如1011 mod 2 = 1),则z mod 2 = 1

尝试

SELECT v FROM ViewProductsList v WHERE MOD(v.statusId/16 , 2) = 1

这篇关于以位AND(&amp;)为标准的JPA @NamedQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 10:09