I am trying to insert data into my database, I am using JPA in my project.
This is what my bean looks like.
@PersistenceContext EntityManager em; em.createNativeQuery("INSERT INTO testtable ('column1','column2') VALUES ('test1','test2')").executeUpdate();
myfacade:
@Stateless public class TestFacade extends AbstractFacade<Test> { @PersistenceContext(unitName = "TEST2PU") private EntityManager em; @Override protected EntityManager getEntityManager() { return em; } public TestFacade() { super(Test.class); }
I get an error message:
javax.persistence.TransactionRequiredException: executeUpdate is not supported for a Query object obtained through non-transactional access of a container-managed transactional EntityManager
and if I do not use @PersistenceContext for EntityManager
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TEST2PU"); EntityManager em = emf.createEntityManager(); em.createNativeQuery("INSERT INTO testtable ('column1','column2') VALUES ('test1','test2')").executeUpdate();
this is my mistake:
javax.persistence.TransactionRequiredException: Exception Description: No externally managed transaction is currently active for this thread
note: you really need to use your own query for this.
galao
source share