【发布时间】:2019-01-28 15:09:02
【问题描述】:
我正在尝试学习 Spring Data REST,但我立即陷入困境。我有两个实体,用户和事件。 User 和 Event 是一对多的关系,所以一个 User 可以有很多 Event。
@Entity
public class {
@OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
private Set<Event> events;
...
}
我有一个对应的 UserRepository
public interface UserRepository extends JpaRepository<User, Long> {
ApplicationUser findByUsername(String username);
}
当我启动项目时,它会暴露几个端点,我尝试了这个
GET http://localhost:8080/api/users/1/events
它有效,我明白了
{
"_embedded": {
"events": []
},
"_links": {
"self": {
"href": "http://localhost:8080/api/applicationUsers/1/events"
}
}
}
然后我尝试添加一个事件
PUT http://localhost:8080/api/users/1/events (with a payload of course)
然后再次发送事件的 GET 请求,但仍然是空的。而且它似乎没有任何插入。
这应该在 Spring Data REST 中默认工作还是我错过了什么?我认为使用 @OneToMany 注释的 Set 应该可以让我向某个用户添加事件。
【问题讨论】:
标签: rest spring-data-jpa spring-data-rest