User1230321
User1230321

Reputation: 1475

How to achieve relationships in spring data couchbase?

I've two documents: TableA and TableB. TableA references TableB, but they cannot be clubbed together. Since they are quite "unrelated" data. Also from TableA to TableB there is some kind of "ManyToOne" relationship. Hence I don't want to put the TableB data inside TableA(redundancy issues). That is my TableA json would look like this:

{
   "class":"tableA",
   "tableB": [
     "1", "2"
   ]
}

TableB json:

[
   {
      "class": "tableB",
      "id": "1",
      "name": "name1"
   },
   {
      "class": "tableB",
      "id": "2",
      "name": "name2"
   }
]

So, in spring data couchbase I've created two different documents: TableA and TableB. My doubt here is, in TableA entity(@Document) should I directly reference TableB, or just keep an array of string(IDs)? If I directly reference TableB, then I'll not be getting the above json. My primary concern here is, I'm going to replicate all the data into a mobile device. I would not want data to be repeated and cause memory issues. Please help.

Upvotes: 1

Views: 527

Answers (1)

prettyvoid
prettyvoid

Reputation: 3696

Relationship annotations like @ManyToOne, etc.. are not supported by spring-data-couchbase and as you already know there is nothing that enforces relationships in Couchbase itself.

I have relations across many entities in my current project and they all just refer to the id/key as string and I manage the loading of each entity by hand.

So I'd say just refer to TableB as a strings array, and either send the mobile the data as is along with needed info from TableB... or create a DTO class and populate it by hand and hand it to your clients.

Upvotes: 1

Related Questions