【发布时间】:2021-06-27 19:06:21
【问题描述】:
我有这个错误:
2020-11-16 22:36:09.313 ERROR 19428 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: collection was evicted; nested exception is org.hibernate.HibernateException: collection was evicted] with root cause
org.hibernate.HibernateException: collection was evicted
at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:43) ~[hibernate-core-5.4.22.Final.jar:5.4.22.Final]
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102) ~[hibernate-core-5.4.22.Final.jar:5.4.22.Final]
我创建了一个 API REST 应用程序,我的实体有这样的组合:
public class Label {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long Id;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "label")
private Set<Release> releases;
public class Release {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "label_id")
private Label label;
在创建发布/标签关联数据的第三个 API-REST 请求之后,我在顶部出现错误
休息控制器:
@PostMapping(value = "/release", consumes = "application/json", produces = "application/json")
Release newRelease(@RequestBody ReleaseDto releaseDto) {
return releaseService.addRelease(releaseDto);
}
发布服务:
@Service("ReleaseService")
public class ReleaseServiceImpl implements ReleaseService{
@Autowired
ReleaseRepository repository;
@Autowired
LabelRepository labelRepository;
@Override
public Release addRelease(ReleaseDto releaseDto) {
Release release = new Release();
Optional<Label> label = labelRepository.findById(releaseDto.getLabel_id());
if(label.isPresent()){
release.setName(releaseDto.getName());
release.setLabel(label.get());
repository.save(release);
}
return release;
}
我很困惑,也许我必须实现一个实体管理器?
您好。
【问题讨论】:
-
你也可以添加你的 addRelease 方法吗?到目前为止看起来还不错。
-
@Thomas 我使用了lombok,在 releaseDto 中我使用了
@Data notation -
我的意思是服务,实现 addRelease。也许您在配置休眠时也有一些不寻常的设置?
-
@Thomas 我是春季的初中生,在我的应用程序中我的 application.properties
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.ddl-auto=update§ spring.datasource.url=jdbc:mysql://IPXX:3306/DB spring.datasource.username=(Username) spring.datasource.password=(Password) -
@Thomas 我确实更新了这项服务。