0

I'm testing spring data rest and I would like make a post on a relation entity.

For exemple :

I've two classes :

one       two 
-----    -----
field     field

@OneToOne 
fieldTwo

how can I instantiate two ?

when I do post on /one


{
  "field":"field",
  "field2": {
      "field":"field"
   }

it doesn't create a field2

when I do post on /one/{idOne}/twos:

"field2": {
      "field":"field"
   }

it does nothing.

Does somebody have more informations ?

I didn't find any informations about this.

Thanks

Gegko

Gegko
  • 15
  • 4

1 Answers1

0

If I understood correctly you're trying to create records/entities with association using Spring Data Rest.

In Spring Data Rest when you POST for an entity, it won't create automatically the associated entity. instead, you will have to create each entity separately by yourself using rest.

If you want to create entities with association using REST, all you have to do is first create the not owning entity (the entity which doesn't hold the foreign key). when you do that you'll have its rest URL.

the second step is to take that URL and to put it as a foreign key when you try to save the second entity.

Here's an example:

POSTing a @OneToMany sub-resource association in Spring Data REST

Community
  • 1
  • 1
Moshe Arad
  • 3,587
  • 4
  • 18
  • 33