0

What is the best way to design a many to many relation with hateoas? I have a bidirectional relation between two classes, defined with a Set.

My problems are the POST / PUT methods for binding one resource to the other. (Making a Relation)

Example:

class A {
    int id;
    String nameOfA = "A";
    Set<B> set;
}

class B {
    int id;
    String nameOfB = "B";
    Set<A> set;
}

First way: resource uris could be

/A/{aid}/B/{bid}
/B/{bid}/A/{aid}

For adding a relation between A with id 1 and B with id 2 I would make a POST on: /A/1/B/2 or /B/2/A/1.

Second way:

For adding a relation between A with id 1 and B with id 2 POST on /A/1/B with a "B-object" as content:

{id:2,nameOfB:"B"}

Which is the better way or are there even better solutions? Thanks for help :)

wot-stefan
  • 91
  • 7
  • sry this doesn't help me. My question is what is the "RESTful" way to solve this? Making Resources like way1 or way2? – wot-stefan May 07 '14 at 18:37
  • Okay. Why not hit an endpoint with `{"A":1, "B":2}`? Same endpoint for all, right? – 2rs2ts May 07 '14 at 18:40
  • If it's directional you could do `{"A":1, "B":2, "to":"B"}` (or `"to":"A"`) – 2rs2ts May 07 '14 at 18:43
  • hmm thats a good point but i don't like it :D, i would need a virtual key for this relation or? - {id:1, "A":1, "B":2} – wot-stefan May 07 '14 at 18:44
  • you mean a link to A/B? – wot-stefan May 07 '14 at 18:48
  • What I'm saying with that is that you have an `A` with the ID `1`, a `B` with the ID `2`, and the direction is from the `A` `"to"` the `B`. You can omit `"to"` if your relation is not directional. – 2rs2ts May 07 '14 at 18:50
  • well the relation is bidirectional. is there. But how a rest client easy finds all a B to the A with the id 1. – wot-stefan May 07 '14 at 18:52
  • If it's bidirectional then just `POST` the JSON `{"A":1, "B":2}` to a hypothetical `relation` endpoint. Super easy. – 2rs2ts May 07 '14 at 18:54

0 Answers0