我正在尝试在我的数据库中插入数据,我在我的项目中使用 JPA。
这就是我的豆子的样子。
@PersistenceContext
EntityManager em;
em.createNativeQuery("INSERT INTO testtable ('column1','column2') VALUES ('test1','test2')").executeUpdate();
我的门面:
@Stateless
public class TestFacade extends AbstractFacade<Test> {
@PersistenceContext(unitName = "TEST2PU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public TestFacade() {
super(Test.class);
}
我收到一个错误:
javax.persistence.TransactionRequiredException: executeUpdate is not supported for a Query object obtained through non-transactional access of a container-managed transactional EntityManager
如果我不使用@PersistenceContext for EntityManager
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TEST2PU");
EntityManager em = emf.createEntityManager();
em.createNativeQuery("INSERT INTO testtable ('column1','column2') VALUES ('test1','test2')").executeUpdate();
这是我的错误:
javax.persistence.TransactionRequiredException:
Exception Description: No externally managed transaction is currently active for this thread
注意:确实需要为此使用本机查询。