Saving and gaining datas whith one-to-many relation
Hi all,
Here are my java-client code:
/** save data **/
client.set(getKey(cls, id), 0, Object);
/** gain data **/
public static T getData(long id, Class<? extends BasePlayerModule> cls)
throws IllegalArgumentException, IllegalAccessException, UnKnowTypeException, InstantiationException,
ClassNotFoundException {
return getCouchBaseData(cls, getKey(cls, id));
}
@SuppressWarnings("unchecked")
private static T getCouchBaseData(Class<? extends BasePlayerModule> cls, String keyName)
throws InstantiationException, IllegalAccessException, ClassNotFoundException, UnKnowTypeException {
String data = (String) client.get(keyName);
...
...
As you can see, the gain method only contain one-one data, I wonder how can i gain datas with one-many relation?
In other words, How do I design this function?
Thanks!
Hello,
It depends how you want to do it and what do you use to do the JSON<->Java part. Which library are you using? (GSon, Jackson, ....)
At the end it is you that make the decision to either embed the document in the parent (1) or create a child document and use the is in the children. (2)
Case 1:
id: person:001
{
"name" : "John",
"children" : [ {"name":"Jane"}, {"name":"David"} ]
}
Chase 2:
id: person:001
{
"name" : "John"
}
id:person:002
{
"name":"Jane",
"parent_id" : "person:001"
}
id:person:003
{
"name":"David",
"parent_id" : "person:001"
}
So you can do it yourself in Java, I am currently working on developing small frameworks such as:
- https://github.com/tgrall/couchbase-document-api (not complete at all but can give you some idea)
- https://github.com/tgrall/hibernate-ogm/tree/couchbase-support to work with JPA (60% done)
regards
Tug
@tgrall