Confusing about JPQL

回眸只為那壹抹淺笑 提交于 2019-12-10 12:19:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!