JPA-RS中的双向OneToOne关系不可遍历

时间:2013-12-05 14:23:04

标签: jpa eclipselink jpa-rs

我使用下面的简单实体来探索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示例可以轻松实现。 据我所知,我应该能够做到以下几点:

  1. 获取ID为1的员工:GET url / entity / Employee / 1:
    {
    "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": []
    }
  2. 获取员工的地址:GET url / entity / Address / 2:
    {
    "city": "A City",
    "id": 2,
    "street": "Street No. 1",
    "_relationships": [
    {
      "_link": {
        "href": "url/entity/Address/2/employee",
        "rel": "employee"
      }
    }
    ]
    }
  3. 现在让我们从这里回到员工:GET url / entity / Address / 2 / employee - > http状态400
  4. 在上面提到的例子中,我可以使用关系的所有链接进行完美导航。

    p.s。:我想标记这个JPA-RS,但我没有足够的声誉来创建新标签。

0 个答案:

没有答案