Hibernate は、「Hibernate によってスローされた例外は、データベース トランザクションをロールバックし、セッションをすぐに閉じる必要があることを意味します」と言います。
persist メソッドが SQLException をスローし、entityManager がダーティになった場合、EntityManager を閉じると、Conversation Scope のままになります。
私が使用している: tomcat 7、cdi 1.1、休止状態 4.1;
現在の会話用に新しい EntityManager を生成してダーティを置き換える方法はありますか?
@Produces
@ConversationScoped
public EntityManager create(EntityManagerFactory emf) {
EntityManager em = emf.createEntityManager();
...
ViewBean
@Named @ConversationScoped
public class MyView implements Serializable {
enter code here
@Inject @Getter private EntityManager em;
...
public void persist(){
try{
getEm().getTransaction().begin();
getEm().persist(entityInstance);
getEm().getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
if(getEm().getTransaction().isActive()){
getEm().getTransaction().rollback();
}
}
}