我使用下面的简单实体来探索jpa-rs:
@Entity
public class Employee {
@Id
@GeneratedValue
private int id;
private String name;
@OneToOne
private Address address;
}
@Entity
public class Address {
@Id
@GeneratedValue
private int id;
private String street;
private String city;
@OneToOne(mappedBy = "address")
private Employee employee;
}
创建一个Employee
关系Address
的实体就像一个魅力:
{ "name": "John Doe", "address": { "street": "Street No. 1", "city": "A City" } }
但我不能像{HATEOAS提议的那样遍历_link
s所产生的对象
Eclipselink wiki(http://wiki.eclipse.org/EclipseLink/Examples)中提供的students
示例可以轻松实现。
据我所知,我应该能够做到以下几点:
{ "id": 1, "name": "John Doe", "_relationships": [ { "_link": { "href": "url/entity/Employee/1/address", "rel": "address" } } ], "address": { "_link": { "href": "url/entity/Address/2", "method": "GET", "rel": "self" } }, "staff": [] }
{ "city": "A City", "id": 2, "street": "Street No. 1", "_relationships": [ { "_link": { "href": "url/entity/Address/2/employee", "rel": "employee" } } ] }
在上面提到的例子中,我可以使用关系的所有链接进行完美导航。
p.s。:我想标记这个JPA-RS,但我没有足够的声誉来创建新标签。