Spring Data REST javax.persistence.RollbackException:提交事务时出错
我有两个实体:Spring Data REST javax.persistence.RollbackException:提交事务时出错,spring,spring-boot,spring-data-jpa,spring-data,spring-data-rest,Spring,Spring Boot,Spring Data Jpa,Spring Data,Spring Data Rest,我有两个实体: @Data @Entity(name = "users") @NoArgsConstructor @RequiredArgsConstructor @EqualsAndHashCode public User { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="id", unique = true, nullable = false) private Integer
@Data
@Entity(name = "users")
@NoArgsConstructor
@RequiredArgsConstructor
@EqualsAndHashCode
public User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id", unique = true, nullable = false)
private Integer userId;
private String userName;
@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
@JoinColumn(name = "user_id")
@Fetch(value = FetchMode.SUBSELECT)
private List<Degree> degreeList;
}
资源包括:
@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRestRepository extends CrudRepository<User, Integer> {
}
我已经搜索并发现我必须保存
Degree
然后更新degreeList
,我该怎么做请指导Spring数据REST是基于URI的(遵循HATEOAS规则)。如果要将“学位”添加到“degreeList”,实际上首先需要创建学位:
HTTP发布到https://www.myhost.com/api/degrees
请求正文必须是内容类型application/json
之后,在响应中返回学位ID。您可以使用此ID将学位添加到列表中:
HTTP发布到https://www.myhost.com/api/users/{userId}/degreeList
重要提示:这次请求内容类型是text/uri list
!正文必须是纯文本,并且应该只包含URI
https://www.myhost.com/api/degrees/{degreeId}
Spring数据REST是基于URI的(遵循HATEOAS规则)。如果要将“学位”添加到“degreeList”,实际上首先需要创建学位: HTTP发布到
https://www.myhost.com/api/degrees
请求正文必须是内容类型application/json
之后,在响应中返回学位ID。您可以使用此ID将学位添加到列表中:
HTTP发布到https://www.myhost.com/api/users/{userId}/degreeList
重要提示:这次请求内容类型是text/uri list
!正文必须是纯文本,并且应该只包含URI
https://www.myhost.com/api/degrees/{degreeId}
是否有其他日志需要调查?您的意思是
StackTrace
?是否有来自StackTrace的其他信息如何检查映射?度中对应的manytoone?@tkslicon在度中没有任何@manytoone
映射!是否有其他日志需要调查?您的意思是StackTrace
?是否有来自StackTrace的其他信息如何检查映射?度中对应的manytoone?@tkslicon在度中没有任何@manytoone
映射!如果我发出如下post请求:{“href”:https://www.myhost.com/api/degrees/41“}
athttps://www.myhost.com/api/users/45/degreeList
,它不会添加新学位,也会清除列表中以前添加的学位。在https://www.myhost.com/api/degrees
我正在成功保存新学位!{“href”:“}不是正确的请求正文格式。这一个在application/json中,而您只需要使用纯文本,如我的回答中所述:)。只需在请求正文中使用URL字符串,并将内容类型指示为Type/uri列表。如果我发出一个post请求,比如:{“href”:https://www.myhost.com/api/degrees/41“}
athttps://www.myhost.com/api/users/45/degreeList
,它不会添加新学位,也会清除列表中以前添加的学位。在https://www.myhost.com/api/degrees
我正在成功保存新学位!{“href”:“}不是正确的请求正文格式。这一个在application/json中,而您只需要使用纯文本,如我的回答中所述:)。只需在请求正文中使用URL字符串,并将内容类型指示为Type/uri列表。那就行了。
{
"timestamp": "2020-04-17T05:16:51.520+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction",
"path": "/api/users/4/degreeList"
}
https://www.myhost.com/api/degrees/{degreeId}