EntityManager persist() method does not insert record to database

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

    Can you post what exception are you getting? I will assume that your error is that your persistence.xml you don't specified your "Chain" Object.

    You can specify using this tag

    false
    

    Or just

     your.package.Chain
    

    Put this above provider tag.

    Also, never set a number for a column tagged as @Id

    When you use method save() with a Id column with setted value, hibernate will try to UPDATE NOT INSERT your data.

    Do this: Create getEntityManager Method

    public EntityManager getEntityManager() {
        return entityManager;
    }
    

    Then

    @Transactional
    public void saveChain(Chain chain) {
    
        chain.setDate(new Date());
        getEntityManager().persist(chain);
    }
    

提交回复
热议问题