0

I have the below select statement

"SELECT new SystemUser(u.id, u.username, u.fullname, u.status, u.group.id, u.group.name, u.organization.id, u.organization.name) FROM SystemUser"

Pojo

public class SystemUser implements Serializable {

private long id;

private String username;

private String password;

private String fullname;

private SimpleStatus status;

private UserGroup group;

private Organization organization;}

and I want to select all data from SystemUser table, but rows where "SystemUser" has no "group" or "organization" are not selected.

How can I select all data using HQL?

3 Answers 3

1

What you use is a constructor query select new SystemUser(...) from SystemUser. This will construct new objects based on the columns you select. I think this is not necessary, you can simply write select u from SystemUser u.

Or even simpler (but not recommended, because this is not JPQL compliant): from SystemUser

Sign up to request clarification or add additional context in comments.

Comments

0
FROM SystemUser WHERE group IS NULL OR organization IS NULL

Should work fine.

Comments

0

I never try it but following the JPQL-HQL documentation I think you could do something like this (not sure if the case statement can return null values or just empty string):

SELECT new SystemUser(
    u.id, u.username, u.fullname, u.status, 
    CASE 
        WHEN u.group IS NOT NULL THEN u.group.id
        ELSE NULL, 
    CASE 
        WHEN u.group IS NOT NULL THEN u.group.name
        ELSE NULL, 
    CASE 
        WHEN u.organization IS NOT NULL THEN u.organization.id
        ELSE NULL, 
    CASE 
        WHEN u.organization IS NOT NULL THEN u.organization.name
        ELSE NULL,
) 
FROM User u

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.