EntityManager persist() method does not insert record to database
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
<exclude-unlisted-classes>false</exclude-unlisted-classes>
Or just
<class>your.package.Chain</class>
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);
}
Try this:
em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();
PS: You should set a generation method for your ID as well.
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