How to use JPA Prepared stat for Insert request in java

I would like to know how to use a prepared statement for an insert query. Usually for the selected query I use as follows.

Query query = JPA.em()
    .createNativeQuery("select item_status from item_details where box_id=:boxnumber");
query.setParameter("boxnumber", boxNumber);

      

But when I use insert query, I cannot use that above.

Query query = JPA.em()
    .createNativeQuery("insert into item_details values(':item_status')");
query.setParameter("item_status", itemstat);

      

I am getting an error like

java.lang.IllegalArgumentException: 
    org.hibernate.QueryParameterException: could not locate named parameter [item_status]
    at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:368) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
    at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:72) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]

      

please help me figure out this problem. thanks in advance

+3


source to share


1 answer


I couldn't test, but you could try this;



Query query = JPA.em().createNativeQuery("insert into item_details(item_status) values(?)") .setParameter(1, itemstat).executeUpdate();

      

+1


source







All Articles