I'm trying to learn how to write RESTful Java applications using Jersey and Hibernate, and I'm struggling to figure out how to handle the parent / child relationship type when sending data to the resource. I use JSON for data exchange, but I do not think this is especially important for my problem.
In the example in which I work with models, the relationship between employees and teams. An employee may or may not be a member of one team:
GET /team/ - returns a list of teams
POST /team/ - creates a new team
GET /team/1 - returns a list of employees in the team with the ID 1
GET /employee/ - returns a list of employees
POST /employee/ - creates a new employee
GET /employee/1 - returns details about the employee with the ID 1
For this, I have a few annotated Hibernate POJOs: one for the team and one for the employee, with a 1-N relationship between them (remember that the employee cannot be a member of the team!). The same POJOs are also annotated as @XmlRootElements so that JAXB allows me to pass them to / from the client as JSON.
The properties for the two objects are as follows:
Team
Long id;
String name;
String desc;
List<Employee> employees;
Employee
Long id;
Team team;
String name;
String phone;
String email;
All still. But I'm struggling to figure out how to make an employee a member of a team during creation, simply by passing in the team identifier, rather than passing in a nested team object in my JSON object.
For example, I would like to be able to call POST / employee / with JSON, which looks like this:
{
"team_id":"1",
"name":"Fred Bloggs",
"phone":"1234567890",
"email":"test@example.com"
}
But instead, I need to pass something like this:
{
"team":{
"id":"1",
}
"name":"Fred Bloggs",
"phone":"1234567890",
"email":"test@example.com"
}
So my question is: how do others handle linking in JSON / REST without going around whole graphs of objects?
Sorry, this is such a fragmentary question, but, as I said, I'm just starting and terminology is a problem for me at this stage!