【发布时间】:2020-07-30 14:12:10
【问题描述】:
我有两个实体:
@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;
}
和
@Data
@Entity(name = "degrees")
@NoArgsConstructor
@RequiredArgsConstructor
@EqualsAndHashCode
public Degree {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id", unique = true, nullable = false)
private Integer degreeId;
private String degreeTitle;
}
RestResources 是:
@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRestRepository extends CrudRepository<User, Integer> {
}
然后:
@RepositoryRestResource(collectionResourceRel = "degrees", path = "degrees")
public interface DegreeRestRepository extends CrudRepository<Degree, Integer> {
}
我必须使用 https://www.myhost.com/api/users/{userId}/degreeList 端点在 foo 的 barList 中添加新的 bar,如图所示:
但我得到了:
{
"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"
}
我搜了一下,发现必须先保存Degree再更新degreeList,请问该怎么做呢
【问题讨论】:
-
还有其他需要调查的日志吗?
-
你的意思是
StackTrace? -
是来自堆栈跟踪的其他信息
-
你检查你的映射怎么样?对应的度数是多少?
-
@tksilicon 在
Degree中没有任何@ManyToOne映射!
标签: spring spring-boot spring-data-jpa spring-data spring-data-rest