EntityManager persist() not saving anything to database
Check your server logs. Are you creating new EntityManger
? and have not begun the transaction. I think, Where you have begun that is another EntityManager
object.
This generally happens when Transaction in not applied.. I doubt @Transactional interceptor is not intercepting properly.
persist()
means "add object to list of managed entries". To save object to data base you must call flush()
method. But remember you must call in inside the transaction.
//Edit: Example save method.
public void save(T t){
// begin transaction
em.getTransaction().begin();
if (!em.contains(t)) {
// persist object - add to entity manager
em.persist(t);
// flush em - save to DB
em.flush();
}
// commit transaction at all
em.getTransaction().commit();
}
This is not the best that you can make, but good enough.