How to start a transaction in JPA using entityManager

前端 未结 4 561
陌清茗
陌清茗 2021-01-02 13:26

I have started working on an application which uses spring, hibernate, JPA, SOAP webservices. Now there is a requirement that certain queries have to be run in a transaction

4条回答
  •  暖寄归人
    2021-01-02 13:49

    Try injecting EntityManagerFactory and then creating the EntityManager manually:

    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    
    public boolean processBills() throws Exception{
    
       EntityManager em = entityManagerFactory.createEntityManager();
    
       EntityTransaction tx = null;
    
       Session session = null;
    
       try{
    
           session = em.unwrap(Session.class);
           tx = em.getTransaction();
    

提交回复
热议问题