简体   繁体   中英

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).

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. 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?

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).

Third question: In SQL I would do something like this to get all users with name, address and roles:

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. 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? What is the best practice when you dealing with many criterias?

Thanks in advance

You can use a native SQL query for this.

JPQL does not directly support bitwise operators, but if you are using EclipseLink you could the SQL operator.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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