简体   繁体   English

JPQL +准则中可以使用此SQL语句吗?

[英]Is this SQL statement possible in JPQL + guidelines?

Background: I have a User entity. 背景:我有一个用户实体。 The User entity looks like: 用户实体看起来像:

private String name;
private String address;
private List<Picture> pictureList; //The users pictures
private int rolesBitmask; //Is a bitmask for around 20 different roles
private int anotherBitmask; //Is similar bitmask as the roles
//
// Maybe 20 more fields..

I will have an AJAX search webpage that will perform JPQL queries on ALL these fields (well maybe not the pictures, but for the most of the fields). 我将拥有一个AJAX搜索网页,该网页将在所有这些字段上执行JPQL查询(也许不是图片,而是大多数字段)。

For example, the web user might be search at a user with name like "An%", lives on Address like "Pica%", have roles "printer", "admin", "guest" and don't care about the rest of the roles available. 例如,网络用户可能正在搜索名称为“ An%”,居住在“ Pica%”之类的地址,具有角色“打印机”,“管理员”,“来宾”的用户,而忽略其他用户可用角色。 The "printer", "admin" and "guest" roles is interpreted to the bitmask according to a predefined value. 根据预定义的值将“打印机”,“管理员”和“来宾”角色解释为位掩码。

My first question: 我的第一个问题:

Is it a bad idea to do these kind of queries in only 1 JPQL / whatever query? 仅用1个JPQL或任何查询进行此类查询是一个坏主意吗?

Second question: If I decide to go on with these "complex" queries what about the performance (keep in mind that it is a AJAX webpage which might be a new database access every time the user is changing the form). 第二个问题:如果我决定继续使用这些“复杂”查询来查询性能呢(请记住,这是一个AJAX网页,每次用户更改表单时,它可能都是新的数据库访问权限)。

Third question: In SQL I would do something like this to get all users with name, address and roles: 第三个问题:在SQL中,我将执行以下操作以使所有用户具有名称,地址和角色:

SELECT * FROM User WHERE (rolesBitmask & queryRoleBitmask) ^ queryRoleBitmask = 0 AND name LIKE 'An%' AND address LIKE 'Pica%';

As fas as I know, JPQL don't support bitwise operations, so I have to use native queries or try to tranform it to a JPQL equivalent, but I do not know how I am supposed to do it. 据我所知,JPQL不支持按位操作,因此我必须使用本机查询或尝试将其转换为等效的JPQL,但我不知道该怎么做。 Do you? 你呢?

Can you guide me here, maybe I have to throw this whole AJAX search web page idea in the thrashcan, or what do you think about it? 您能在这里指导我吗,也许我必须将整个AJAX搜索网页的想法扔进thrashcan,或者您对此有何看法? What is the best practice when you dealing with many criterias? 当您处理许多标准时,最佳实践是什么?

Thanks in advance 提前致谢

You can use a native SQL query for this. 您可以为此使用本机SQL查询。

JPQL does not directly support bitwise operators, but if you are using EclipseLink you could the SQL operator. JPQL不直接支持按位运算符,但是如果使用EclipseLink,则可以使用SQL运算符。

Select u from User u where SQL('((? & ?) ^ ?)', u.rolesBitmask, u.queryRoleBitmask, u.queryRoleBitmask) = 0 and u.name LIKE 'An%' AND u.address LIKE 'Pica%'

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#SQL http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#SQL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM