0

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 Answer 1

3

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.

2
  • Thanks alot That helped me alot :)
    – krs8785
    Oct 20, 2014 at 14:53
  • NOT IN is supported by HQL, so simply referring to objects should work, smth like: select art from Artifact art where art.Id not in (select c.Artifact_id from Classification c where c.active = 1)
    – Alla
    Oct 21, 2014 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.