java - EntityManager 合并()挂起
问题描述
当我尝试通过多个调用合并多个实体时,我的服务似乎在尝试背靠背合并时第二次挂起,但是如果我只用任何一个实体调用这个实体,例如service.updateFoo(foo1);
或service.updateFoo(foo2);
这是我尝试过的
服务:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
@Service
public class FooService {
private final EntityManagerFactory entityManagerFactory;
private final FooRepository fooRepository;
@Autowired
public FooService(
EntityManagerFactory entityManagerFactory, FooRepository fooRepository) {
this.entityManagerFactory = entityManagerFactory;
this.fooRepository = fooRepository;
}
public Foo updateFoo(Foo foo) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
try {
foo = entityManager.merge(foo); // hangs here the 2nd time
} finally {
entityManager.flush();
entityTransaction.commit();
entityManager.clear();
}
return foo;
}
}
回购
@Repository
public interface FooRepository extends CrudRepository<Foo, Long> {
}
实体
@Setter
@Getter
@Entity
@Table(name = "foo")
public class Foo implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NonNull private String userId;
@Embedded private FooCaller fooCaller;
@NonNull
@Enumerated(EnumType.STRING)
private FooStatus status;
}
我做错了什么?
解决方案
推荐阅读
- java - 从 CSV 文件读取并将每一行写入不同的文件
- r - R:使用 ggplot2 将变量添加到条形图
- r - 未更改任何内容后未加载文件
- c# - FluentValidation 和自定义消息,告诉用户允许/预期哪些值
- python - 我可以将 Slack 事件 API 与斜杠命令一起使用吗?
- c - 重置日志文件后双重释放或损坏(顶部)
- c# - 为什么 IE 的 selenium webdriver 无法单击具有特定 CSS 的 iframe 中的元素?
- javascript - 在数据中使用特殊字符时,Json 解析会出错
- regex - 正则表达式 - 贪婪直到下一场比赛
- r - 如果模式匹配,则为列名添加前缀