【问题标题】:Inserting in my JPA using entity manager native query使用实体管理器本机查询插入我的 JPA
【发布时间】:2013-05-24 04:50:50
【问题描述】:

我正在尝试在我的数据库中插入数据,我在我的项目中使用 JPA。

这就是我的 bean 的样子。

@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

注意:确实需要为此使用本机查询。

【问题讨论】:

  • 这个问题与JSF完全无关。
  • 您开始交易了吗?或者你用什么来管理事务?

标签: jpa entitymanager


【解决方案1】:

您可以使用 NativeQuery 及其 executeUpdate 方法来做到这一点:

String query = "insert into Employee values(1,?)";

em.createNativeQuery(query)
   .setParameter(1, "Tom")
   .executeUpdate();

【讨论】:

  • 如何使用此查询获取最后插入的 id?
  • @KumaresanPerumal 你已经发现了吗?也想知道这是否可能
  • @Kumaresan Perumal "插入表 (a, b) 值 ('a', 'b') 返回 id;"尝试将它与 getResultList() 和 postgresql 结合使用。
【解决方案2】:

我有同样的问题。这是解决方案。

EntityManager em = getEntityManager();
EntityTransaction et = em.getTransaction();
et.begin();
em.createNativeQuery("UPDATE ... ;").executeUpdate();
et.commit();

【讨论】:

    【解决方案3】:
     String message = "";
        try
        {
            EntityManager em = emf.createEntityManager();
            if (objUser.getId() <= 0)
            {
                em.getTransaction().begin();
                em.createNativeQuery("INSERT INTO userinfo ( login, upassword, email, mobile, fax, dob)"
                        + " VALUES ( :a, :b, :c, :d, :e, :f)")
                        .setParameter("a", objUser.getLogin())
                        .setParameter("b", objUser.getUpassword())
                        .setParameter("c", objUser.getEmail())
                        .setParameter("d", objUser.getMobile())
                        .setParameter("e", objUser.getFax())
                        .setParameter("f", objUser.getDob()).executeUpdate();
                em.getTransaction().commit();
                em.close();
                message = "Success";
            }
        } catch (HibernateException ex)
        {
            message = ex.getMessage();
        }
    

    【讨论】:

    • 一些解释会显着提高您的回答质量。
    • 我们如何为批量插入做到这一点
    【解决方案4】:

    假设您使用的是容器管理的entityManager(注入@PersistenceContext),您只是错过了TestFacade 方法上方的@Transactionnal 注释。

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 1970-01-01
      • 2016-07-02
      • 2015-01-30
      • 1970-01-01
      • 2019-11-09
      • 2015-07-24
      • 2011-03-23
      • 1970-01-01
      相关资源
      最近更新 更多