【发布时间】:2015-03-14 23:19:59
【问题描述】:
我的项目中定义了以下实体:
国家
@Entity
@Data
public class Country {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column(nullable = false)
String name;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
List<City> cities = new ArrayList<City>();
}
城市
@Entity
@Data
public class City {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column(nullable = false)
String name;
@ManyToOne
Country country;
}
人物
@Entity
@Data
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column
String name;
@Embedded
Address address = new Address();
}
地址
@Data
public class Address {
@Column
String line;
@ManyToOne
Country country;
@ManyToOne
City city;
}
我还为Person、Country 和City 定义了存储库。
当我向 /persons/1 发出 GET 请求时,我得到以下结果:
{
"name":null,
"address":{
"line":"Address1"
},
"_links":{
"self":{
"href":"http://localhost:8080/persons/1"
},
"city":{
"href":"http://localhost:8080/persons/1/city"
},
"country":{
"href":"http://localhost:8080/persons/1/country"
}
}
}
我怀疑由于地址是一个嵌入对象,生成的国家和城市链接是错误的。尽管存在 city 和 country 值,但它们不会返回任何内容。正确的链接应该是什么?
Spring Data Rest 不支持嵌入对象吗?
【问题讨论】:
-
我认为您无法联系到
/persons/1/address? -
它返回 HTTP 400 错误请求,并带有以下消息:PersistentEntity 不能为空!
标签: spring rest spring-boot spring-data-jpa spring-data-rest