Search code examples
javamarklogicpojo

MarkLogic POJO Data Binding Interface: JSONMappingException when performing a POJO search


I'm working with the MarkLogic POJO Databinding Interface at the moment. I'm able to write POJOs to MarkLogic. Now I want to search those POJOs and retrieve the search results. I'm following the instructions from: https://docs.marklogic.com/guide/java/binding#id_89573 However, the search results don't seem to return the correct objects. I'm getting a JSONMappingException. Here's the code:

    HashMap<String, MatchedPropertyInfo> matchedProperties = new HashMap<String, MatchedPropertyInfo>();
    PropertyMatches PM = new PropertyMatches(123,"uri/prefix/location2", "uri/prefix", 1234,0,"/aKey","/aLocation",true,matchedProperties);
    MatchedPropertyInfo MPI1 = new MatchedPropertyInfo("matched/property/uri1", "matched/property/key1", "matched/property/location1", true,"ValueMatch1", 12, 1*1.0/3, true);
    MatchedPropertyInfo MPI2 = new MatchedPropertyInfo("matched/property/uri2", "matched/property/key2", "matched/property/location2", true,"ValueMatch2", 14, 1.0/2.0, true);
    PM.getMatchedProperties().put("matched/property/prefix/location1", MPI1);
    PM.getMatchedProperties().put("matched/property/prefix/location2", MPI2);

    PojoRepository myClassRepo = client.newPojoRepository(PropertyMatches.class, Long.class);
    myClassRepo.write(PM);

    PojoQueryBuilder qb = myClassRepo.getQueryBuilder();
    PojoPage<PropertyMatches> matches = myClassRepo.search(qb.value("uri", "uri/prefix/location2"),1);
    if (matches.hasContent()) {
        while (matches.hasNext()) {
            PropertyMatches aPM = matches.next();
            System.out.println("  " + aPM.getURI());
        }
     } else {
        System.out.println("  No matches");
     }

The PropertyMatches (PM) object is succesfully written to the MarkLogic database. This class contains a member: private String URI which is initiated with "uri/prefix/location2". The matches.hasContent() returns true in the example above. However, I'm getting an error on PropertyMatches aPM = matches.next();


Solution

  • Searching POJOs in MarkLogic and read them into your Java program requires the POJOs to have an empty constructor. In this case PropertyMatches should have public PropertyMatches(){} and MatchedPropertyInfo should have public MatchedPropertyInfo(){}