users@jersey.java.net

[Jersey] Best practice to handle JPA relation

From: Florian Purnhagen <florian.purnhagen_at_googlemail.com>
Date: Thu, 24 Nov 2011 18:22:56 +0100

Hello,

I have a question concerning the best practice to handle persisted objects
that have a relation to other objects:
Simple example:
Assume I have an entity class like this:

@XmlRootElement
@Entity
public class Department {
@Id
@GeneratedValue
long id;
@OneToMany
List<Employee> employees;
}


Another one:

@Entity
public class Employee {
@Id
@GeneratedValue
long id;
String name;
}


Now I have persisted some objects into my JPA Database: I have one
Department and two Employees in it.
When I return the Department via Jersey to the Browser, I get this result:

{"employees":[{"id":"8","name":"john"},{"id":"9","name":"jane"}],"id":"10"}



Fine. But very verbose, especialy when my employees have more properties,
or even relations to other entities.
So I would like to limit the output of all my "Sub"-Objects to the ID only.
Something like this:


{"employees":[{"id":"8"},{"id":"9"}],"id":"10"}



What is the best practice to achive this?

Best regards, Florian