Reputation: 41
I am using Spring Boot as my backend framework and I have a PERSON
entity which has a one-to-one relation with GENDER
entity.
I am using @RepositoryRestResource
for PersonRepository
and GenderRepository
.
The GENDER
table is already filled with two records of MALE and FEMALE and I have an angular form which creates a new PERSON.
In angular form, there is a list of genders with two items gotten from GenderRepository
(male/female). how can I put the genderId in JSON of POST Request body to create a new Person
.
I see that most of people use text/uri-list to update existing records however I want to set it while creating not updating.
Upvotes: 1
Views: 314
Reputation: 350
You can do that by giving a link to the gender entity.
Person Entity
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
@OneToOne
private Gender gender;
//Other columns and getter/setters are removed for brevity
}
Gender Entity
@Entity
public class Gender {
@Id
@GeneratedValue
private Long id;
@Column
private String gender;
}
The following POST request to localhost:8080/api/persons with Content-Type
header set to application/json
creates a new Person entity and sets it's gender to the Gender with the id of 1.
{
"name": "Mahsum",
"surname": "Demir",
"gender": "http://localhost:8080/api/genders/1"
}
Upvotes: 1