23

I am trying to insert a data in 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:

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 dont 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 error:

javax.persistence.TransactionRequiredException: 
Exception Description: No externally managed transaction is currently active for this thread

note: really need to use native query for this.

2
  • This problem is not related with JSF at all. Commented May 29, 2013 at 5:50
  • did you start transaction ? or what are you using for transaction management ? Commented May 29, 2013 at 6:31

5 Answers 5

15

You can do it using NativeQuery and its executeUpdate method:

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

em.createNativeQuery(query)
   .setParameter(1, "Tom")
   .executeUpdate();
Sign up to request clarification or add additional context in comments.

3 Comments

how to get last inserted id using this query?
@KumaresanPerumal did you already found out? Also wondering whether that's possible
@Kumaresan Perumal "insert into table (a, b) values ('a', 'b') returning id;" Try using that in combination with getResultList() and postgresql.
10

I had same problem. Here is solution.

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

Comments

6

You can use createNativeQuery to write a query specific to the database language.

String query = "INSERT INTO userinfo ( login, upassword, email, mobile, fax, dob)"
             + " VALUES ( :a, :b, :c, :d, :e, :f)"

em.createNativeQuery(query)
    .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();

2 Comments

Some explanations would significantly improve the quality of your answer.
how can we d this for bulk inserts
5

Assuming you're using the container-managed entityManager (injected with @PersistenceContext) you just miss an @Transactionnal annotation above your TestFacade method.

Comments

0
String query = "SELECT * FROM daily_clouser_report_summery b "
                + "WHERE operatorId='" + operatorId + "' AND "
                + "STR_TO_DATE(b.repDate, '%d-%m-%Y')"
                + "BETWEEN STR_TO_DATE('" + formattedDates + "', '%d-%m-%Y') AND STR_TO_DATE('" + formattedDates + "', '%d-%m-%Y');";
        SessionFactory sf = this.hibernateTemplate.getSessionFactory();
        EntityManager em = sf.createEntityManager();
        List<DailyClouserReportSummery> list = em.createNativeQuery(query, DailyClouserReportSummery.class).getResultList();
        return list;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.