如何在@postConstruct中正确创建实体?
我尝试在Springboot应用中@PostConstruct中创建一些实体
@PostConstruct
void load() {
for (int i = 0; i < 10; i++) {
Item item = new Item();
item.setRating(Math.random());
imageList.add(new Image());
item.setImages(imageList);
itemRepository.save(item);
}
,我有错误分离的实体传递给了持久:com.amr.project.model.entity.image;
我尝试添加 @transactional @transactional不要与@postconstrcut一起使用,并且不起作用。
我如何正确创建实体?
I try to create some entity in @PostConstruct in my SpringBoot app
@PostConstruct
void load() {
for (int i = 0; i < 10; i++) {
Item item = new Item();
item.setRating(Math.random());
imageList.add(new Image());
item.setImages(imageList);
itemRepository.save(item);
}
I have error detached entity passed to persist: com.amr.project.model.entity.Image;
I tried adding @Transactional but i find that @Transactional dont work with @PostConstrcut and it`s dont work.
How i can create entity properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如I_AM_PAUL评论的那样,JPA实体不是弹簧bean,因此@PostConscont永远不会被调用。
尝试使用@preload JPA注释
As I_AM_PAUL commented, JPA Entities are not Spring beans, so @PostConstruct won't ever be called.
Try using @PreLoad JPA annotation