I am trying to run an mysql sql statement using the code below;
public void createUpdate(Tsaleline entity) {
//Insert
String sql = "Insert into tSaleLine (CenterId, ProductId, Price, VATRate) "
+ "Select :centerId, :productId, :price, :vatrate "
+ "where (Select count(*) from tSaleLine where CenterId = :centerId and ProductId = :productId)=0; "
//Update
+ "Update tSaleLine "
+ "set CenterId = :centerId, "
+ "ProductId = :productId, "
+ "Price = :price, "
+ "VATRate = :vatrate "
+ "where CenterId = :centerId and ProductId = :productId; ";
Query q = getEntityManager().createNativeQuery(sql);
q.setParameter("centerId", entity.getCenterId())
.setParameter("productId", entity.getProductId())
.setParameter("price", entity.getPrice())
.setParameter("vatrate", entity.getVATRate())
.executeUpdate();
}
}
but it rises a error :
exception
org.apache.jasper.JasperException: javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query
This will only insert if the record does not exist. Afterwards it will update price and vatrate for the specified record.
This is not the ideal solution, because the JPA implementation you are using should be taking care of an INSERT or UPDATE to an entity.