开发者

JQPL: Create new Object within the Query from multiple tables

开发者 https://www.devze.com 2023-04-12 11:15 出处:网络
I\'m currently trying to fetch data with JQPL. My Query looks like SELECT NEW com.test.CustomObject(t1.name, CASE WHEN(t2 IS 开发者_C百科NOT NULL) THEN true ELSE false END) FROM table1 t1, table2 t2

I'm currently trying to fetch data with JQPL. My Query looks like

SELECT NEW com.test.CustomObject(t1.name, CASE WHEN(t2 IS 开发者_C百科NOT NULL) THEN true ELSE false END) FROM table1 t1, table2 t2 WHERE t1.id = :id1 AND t2.id = :id2

Of course the two entities iteself exist an querying for one of them is working. Querying with a fixed value also works:

SELECT NEW com.test.CustomObject(t1.name, false) FROM table1 t1 WHERE t1.id = :id1

My problem is, that I receive no return value if :id2 has a non existing value. Instead of giving false to the CustomObject constructor, the row itself is omitted completely.

What else could I do to gain knowledge if parameter id2 has a row in table2 to pass this as a boolean value to the custom constructor?

Further information can of course be provided.


You have to use LEFT (OUTER) JOIN to connect from table1 to table2. This will cause left hand side to be part of the result also when right hand side of join doesn't exists. In such a case values coming from the right hand side (for example t2.id) have null values.

Also it doesn't make sense to set id as a parameter twice (assuming what you want is t1.id=t2.id). Just use join and set id parameter once.


I could solve the problem with Exsists:

SELECT NEW com.test.CustomObject(t1.name, CASE WHEN EXISTS(SELECT t2.id FROM table2 t2 WHERE t2.id = :id2) THEN true ELSE false END) FROM table1 t1 WHERE t1.id = :id1
0

精彩评论

暂无评论...
验证码 换一张
取 消