EntityManager persist() method does not insert record to database

前端 未结 9 961
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 11:52

I have problem with using EntityManager.persist(Object) method. Now when i get rid of other problems, by app work without Exception but object is not put in my data

9条回答
  •  无人共我
    2021-02-04 12:31

    You can open the new Transaction and then commit your records.

    @PersistenceUnit(unitName = "NameOfPersistenceUnit")
    private EntityManagerFactory entityManagerFactory;
    
    void someMethod(AccountCommodity accountCommodity){
         EntityManager entityManager = entityManagerFactory.createEntityManager();
         entityManager.getTransaction().begin();
         entityManager.persist(accountCommodity);
         entityManager.flush();
         entityManager.getTransaction().commit();
         entityManager.close();
    }
    

提交回复
热议问题