EntityManager persist() method does not insert record to database

前端 未结 9 940
没有蜡笔的小新
没有蜡笔的小新 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:36

    I was able to fix the same problem by adding the Entity to persistence.xml:

    <persistence-unit name="OwnerPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.test.domain.local.Entity</class>
        <exclude-unlisted-classes />
        </persistence-unit>
    </persistence>
    
    0 讨论(0)
  • 2021-02-04 12:39

    Try this:

    em.getTransaction().begin();
    em.persist(entity);
    em.getTransaction().commit();
    

    PS: You should set a generation method for your ID as well.

    0 讨论(0)
  • 2021-02-04 12:48

    Are you getting a specific exception? It would be helpful to know what it is if you are.

    There are a number of similar problems to this already on Stackoverflow:

    Spring transactional context doesn't persist data

    Spring, Hibernate & JPA: Calling persist on entitymanager does not seem to commit to database

    These suggest that you should try adding em.flush() after the em.persist(chain), and altering the @transactional annotations

    You should also check that you have enabled transactional annotations through a line such as :

    <tx:annotation-driven transaction-manager="transactionManager"/> 
    

    in your .xml configuration

    0 讨论(0)
提交回复
热议问题