mapping - marklogic pojo databinding without annotate -
is possible define mapping without using annotate? mentioned below:
@id or @pathindexproperty(scalartype = scalartype.string)
i want define binding in external file , newpojorepository use in order mapping.
thanks.
pinna
you can create new class extends independent pojo object , attach pojo annotations new class. here's example. remember create path range index of type int on managedcat/averageheight.
cat.java
public class cat { private string id; private int averageheight; public cat() {} public cat(string id, int averageheight) { this.id = id; this.averageheight = averageheight; } public string getid() { return id; } public void setid(string id) { this.id = id; } public int getaverageheight() { return averageheight; } public void setaverageheight(int averageheight) { this.averageheight = averageheight; } public string tostring() { return "id=[" + id + "] averageheight=[" + averageheight + "]"; } }
managedcat.java
import com.marklogic.client.pojo.annotation.id; import com.marklogic.client.pojo.annotation.pathindexproperty; import com.marklogic.client.pojo.annotation.pathindexproperty.scalartype; public class managedcat extends cat { public managedcat() { super(); } public managedcat(string id, int averageheight) { super(id, averageheight); } @id @override public string getid() { return super.getid(); } @pathindexproperty(scalartype = scalartype.string) @override public int getaverageheight() { return super.getaverageheight(); } }
catmanager.java
import com.marklogic.client.databaseclient; import com.marklogic.client.databaseclientfactory; import static com.marklogic.client.databaseclientfactory.authentication.digest; import com.marklogic.client.pojo.pojopage; import com.marklogic.client.pojo.pojoquerydefinition; import com.marklogic.client.pojo.pojorepository; import static com.marklogic.client.pojo.pojoquerybuilder.operator.gt; import java.util.arraylist; import java.util.list; public class catmanager { private databaseclient client = databaseclientfactory.newclient( "localhost", 8000, "admin", "admin", digest); private pojorepository<managedcat, string> catrepo = client.newpojorepository(managedcat.class, string.class); private pojoquerydefinition largecatquery = catrepo.getquerybuilder().range("averageheight", gt, 20); private long start = 1; public void savecat(managedcat cat) { catrepo.write(cat); } public cat getcat(string id) { return catrepo.read(id); } public list<cat> getlargecats() { arraylist list = new arraylist<cat>(); catrepo.search(largecatquery, start) .foreach(cat -> list.add(cat)); return list; } public static void main(string[] args) { catmanager catmgr = new catmanager(); catmgr.savecat( new managedcat("housecat", 10) ); catmgr.savecat( new managedcat("lion", 45) ); system.out.println("saved house cat=[" + catmgr.getcat("housecat")+ "]"); system.out.println("saved lion=[" + catmgr.getcat("lion")+ "]"); system.out.println("large cats=[" + catmgr.getlargecats() + "]"); } }
Comments
Post a Comment