问题
I'm trying to pass an array of Category
POJOs to the query using IN
in the SQL:
public ShareObject[] search(String name, Category[] categories) {
...
OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>("SELECT FROM ShareObject WHERE name LIKE ? AND categories IN ?");
List<ODocument> result = db.command(query).execute(name, categories);
This will return an empty list. If I change the SQL to the following I get a result:"SELECT FROM ShareObject WHERE name LIKE ? AND categories IN [#10:0,#10:1]"
I also tried this, without sucess:
OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>("SELECT FROM ShareObject WHERE name LIKE ? AND categories IN ?");
List<ODocument> result = db.command(query).execute(name, new String[] { "#10:0", "#10:1" });
- How do I have to pass the array to the query, IDs or objects?
- Do I have to change the SQL statement?
- Is this even possible?
回答1:
Try this code:
OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>("SELECT FROM ShareObject WHERE name like ? AND categories IN (?)");
List<ODocument> result = db.command(query).execute(name,"[#10:0,#10:1]");
回答2:
You can try this:
String category="[";
Category[] categories={new Category("#11:3"),new Category("#11:0")};
for(int i=0;i<categories.length;i++){
if(i==categories.length-1)
category+=categories[i].getRid()+"]";
else
category+=categories[i].getRid()+",";
}
OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<ODocument>("SELECT FROM ShareObject WHERE name like ? AND categories IN (?)");
List<ODocument> result = db.command(query).execute(name,category);
来源:https://stackoverflow.com/questions/34391006/orientdb-passing-an-array-to-a-query-using-in-on-an-otype-linklist-field