I have used this function to generate bulk insert to MySQL 5.7 with insert or update
private String bulkInsertQuery(List<ProducDetails> productDetailsList){
StringBuilder prodBatchInsert=new StringBuilder("INSERT INTO product_details(prod_id, prod) VALUES ");
StringBuilder prodBatchInsertOnDuplicateKey=new StringBuilder(" ON DUPLICATE KEY UPDATE prod=VALUES(prod)");
productDetailsList.forEach((ProductDetails row)->{
prodBatchInsert.append("(");
prodBatchInsert.append("\""+row.getProdId()+"\"");
prodBatchInsert.append(",");
prodBatchInsert.append("\""+row.getProd()+"\"");
prodBatchInsert.append(")");
prodBatchInsert.append(",");
});
String insertSql=prodBatchInsert.toString();
return insertSql.substring(0,insertSql.length()-1).concat(prodBatchInsertOnDuplicateKey.toString());
}
Query query=entityManager.createQuery(bulkInsertQuery(productDetailsList));
query.executeUpdate();
initially, I used single quotes but it fails due to this type of data product names like ABC'S FOOD which contained single quotes, then I switched to the above double quotes
I want to know if there is a way to resolve this or use parameterized queries to bulk insert on update duplicate keys.