问题
I want to JPQL likes following example:
Let Hotel object has two objects of Customer and Organization. If Customer is not null, organization will be null. And if the Customer is null , organization will not be null. So
I JPQL likes this;
Select h.customer.name.firstName, h.organization.name from Hotel h
i want to assign one of these two result only to one variable because one is always null. Thanks and Sorry for my english.
回答1:
Probably, you are looking for COALESCE()
function. Try the following:
SELECT COALESCE(h.customer.name.firstName, h.organization.name) FROM Hotel h
You also can use CASE
expression if you want.
More details: http://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html/ch11.html#d5e3257
回答2:
You can select both fields & then can check for null values in code, else you can try using case statement, selecting non-null value.
SELECT CASE WHEN (h.customer.name.firstName NOT NULL) THEN h.customer.name.firstName
WHEN (h.organization.name NOT NULL) THEN h.organization.name
ELSE DEFAULT_VALUE
END
FROM Hotel h
[I haven't tried executing it, you can try it with modifications as required]
来源:https://stackoverflow.com/questions/23774916/confusing-about-jpql