How to check NULL or not valid in HQL query

孤街浪徒 提交于 2019-12-11 15:24:56

问题


I have two different table artifact and classification. I want to retrieve list of artifacts who dont have classification. The classification table contains ID(PK), Artifact_Id(foreign key), Active(0/1) while the artifact table contains ID(PK),Name. Each artifact can have many classification. I want to return a list of artifacts when there is no classification for it (i.e. classification does not contain that artifact or when the artifact has classification but all of instances are not active(0))

For example

Artifact
Id Name
1  xyz
2  pqr
3  abc

Classification
Id Artifact_id active
a1 1            0 
a2 1            0
a3 1            0
a4 3            0
a5 3            1

In the above case the artifact 1 and 2 has no classification but 3 has classification since one instance is active(1). So I want to return 1 and 2. I am not sure how to query this. This is what I have tried (not too much though and is wrong)

def list = findAll("from artifact as a full outer join classification as c on a.id=c.artifact_id where active == 0 OR c.aritfact_id is NULL,[max:limit, offset:startPos])

I also tried something in sql

select *  from ARTIFACT full outer join classification  on artifact.id = classification.ARTIFACT_ID where sum(classification.active) == 0 OR classification.aritfact_id is NULL

回答1:


In SQL you could do this with

SELECT * FROM Artifact WHERE Id NOT IN ( SELECT Artifact_id FROM Classification WHERE active = 1);

Basically, you get a list of all Artifact Ids that have an active classification and then get only the Artifacts that are not in that list, i.e. inactive classification or no classification at all.



来源:https://stackoverflow.com/questions/26467420/how-to-check-null-or-not-valid-in-hql-query

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