Simulating self joins in XPath

2
I need to get a list of objects of a given metatype which have a certain attribute identical to that of any other object of the same type, i.e. is a duplicate entry in a list. In SQL I should have written either SELECT jobNumber FROM Job AS j1 INNER JOIN Job AS j2 ON j1.jobNumber = j2.jobNumber WHERE job1.id != job2.id or SELECT job.jobnNmber FROM Job j GROUP BY j.jobnumber HAVING COUNT (*) > 1 How can I achieve this in an XPath query for use in a Java action?
asked
3 answers
3

In Microflow I use the following Xpath to achieve this:

[id != $metaobjectname]

Maybe this will also work within Java actions.

answered
2

If you are searching for duplicate keys, i can imagine you can avoid the problem by defining uniqueness constraints in the domain model of your application? This way you do not need an java action at all.

answered
1

I think this cannot be expressed using XPath, so the only solution i see is retreive all objects, and create a java action which iterates them to check for double items.

this should be something like

List<IMendixObject> objects = Core.retrieveXPathQuery(context, "//"+entityname);
for(IMendixObject object1 in objects ) {
  for(IMendixObject object2 in objects) {
    if (object1.getMemberValue("key") == object2.getMemberValue("key")) {
      //duplicate
} } }

a, depending on the size of your collection, faster approach might be

List<IMendixObject> objects = Core.retrieveXPathQuery(context, "//"+entityname);
for(IMendixObject object1 in objects ) {
  List<IMendixObject> objects2 = Core.retrieveXPathQueryEscaped(context, "//%s[%s='%s']", entityname, "key", object1.getMemberValue("key")); 
    if (objects2.size() > 1) {
      //duplicate
} }

p.s. I did not verify the function name, might be an error in there

answered