我尝试从一个简单的spring数据休息应用程序开始,并努力解决多个OneToMany关系。我有一个人有很多项目和许多地址。要启动并运行应用程序,我必须在项目和地址中设置@RestResource注释以创建正确的链接。但遗憾的是链接不起作用(我得到一个空的结果),为什么我不能在person实体中设置@RestResource注释。这是我的代码。我希望你能帮助我。
人:
@Entity
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int id;
private String name;
//bi-directional many-to-one association to Item
@OneToMany
@JoinColumn(name="id")
private List<Item> items;
//bi-directional many-to-one association to Address
@OneToMany
@JoinColumn(name="id")
private List<Address> addresses;
public Person() {
}
// getter setter
地址
@Entity
public class Address implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int id;
private String address;
//bi-directional many-to-one association to Person
@ManyToOne
@JoinColumn(name="person_id")
@RestResource(rel="address", path="address",exported=true)
private Person person;
public Address() {
}
// getter setter
项目
@Entity
@Table(name="items")
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private int id;
@Column(name="item_name")
private String itemName;
//bi-directional many-to-one association to Person
@ManyToOne
@JoinColumn(name="person_id")
@RestResource(rel="item", path="item",exported=true)
private Person person;
public Item() {
}
// getter setter
我得到了这个结果
{
"name" : "dave",
"items" : [ {
"itemName" : "ite,m"
} ],
"addresses" : [ {
"address" : "myaddress1"
} ],
"_links" : {
"self" : {
"href" : "http://localhost:8090/person/1"
},
"person" : {
"href" : "http://localhost:8090/person/1"
},
"address" : {
"href" : "http://localhost:8090/person/1/address"
},
"item" : {
"href" : "http://localhost:8090/person/1/item"
}
}
}%