【发布时间】:2014-08-05 15:53:34
【问题描述】:
我有一个使用 Spring 和 HATEOAS 的非常基本的系统,但我发现了一个问题。我有两个非常基本的实体,一辆车和一个人。 Getter 和 setter 避免让问题更具可读性。
@Entity
public class Car implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long carId;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="personId")
private Person owner;
private String color;
private String brand;
}
@Entity
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long personId;
private String firstName;
private String lastName;
@OneToMany(mappedBy="owner")
private List<Car> cars;
}
这是我的仓库:
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
}
@RepositoryRestResource(collectionResourceRel = "cars", path = "cars")
public interface CarRepository extends PagingAndSortingRepository<Car, Long> {
List<Person> findByBrand(@Param("brand") String name);
}
我可以创建和查询它们,但一些参考链接已损坏。例如,几个 POST 成功创建了两个相关实体:
http://localhost:8080/people
{ "firstName" : "Frodo", "lastName" : "Baggins"}
http://localhost:8080/cars
{ "color":"black","brand":"volvo", "owner":"http://localhost:8080/people/1"}
这是对他们的 GET 回复:
http://localhost:8080/cars/2
{
color: "black2",
brand: "volvo2",
_links: {
self: {
href: "http://localhost:8080/cars/2"
},
owner: {
href: "http://localhost:8080/cars/2/owner"
}
}
}
http://localhost:8080/people/1
{
firstName: "Frodo",
lastName: "Baggins",
_links: {
self: {
href: "http://localhost:8080/people/1"
},
cars: {
href: "http://localhost:8080/people/1/cars"
}
}
}
但不知道车主为什么车上有这个网址:
http://localhost:8080/cars/2/owner
这实际上不起作用。
有什么帮助吗?
【问题讨论】: