[ACCEPTED]-Insert with Hibernate native query does not work for java.util.Date-jpa

Accepted answer
Score: 13

Don't use concatenation to insert data into 3 queries, use parameters instead. It solves 2 problem with wrong representation of values, as 1 well as many other problems:

entityManager.createNativeQuery(
    "INSERT INTO TASK_ASSESSMENT (ACTIVE_FLAG, ASSESSMENT_DATE, DESCRIPTION, "
    + "TITLE, NEEDS_LEVEL_ID, PATIENT_ID, USER_ID) VALUES (?, ?, ?, ?, ?, ?, ?)")
    .setParameter(1, true)
    .setParameter(2, saveDate, TemporalType.TIMESTAMP) // Since you want it to be a TIMESTAMP
    .setParameter(3, description)
    .setParameter(4, title)
    .setParameter(5, needsLevelId)
    .setParameter(6, patientId)
    .setParameter(7, userId) 
    .executeUpdate();
Score: 0

Looks like a few issues. Some of your fields 7 should have quotes around them. Also, possibly 6 you need to format the timestamp in a different 5 way, not sure how mysql expects it?

Query persistableQuery = entityManager.createNativeQuery(
    "INSERT INTO TASK_ASSESSMENT 
        (ACTIVE_FLAG, ASSESSMENT_DATE, DESCRIPTION, "
    + "TITLE, NEEDS_LEVEL_ID, PATIENT_ID, USER_ID) VALUES ("
        + true +", " 
    + "'" + timeStampDate + "'"
    + ", " 
    + "'" + description + "'"
    + ", " 
    + "'" + title + "'"
    + ", "                                                            
    + "'" + needsLevelId + "')");

As far 4 as formatting the date, I suspect you will 3 need to look at the SimpleDateFormat class, which 2 will let you get the date into whatever 1 format mysql expects. See http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

Score: 0

You can send parameter in method save, or 1 what you use and use named SQL queries

  Query persistableQuery = entityManager.createNativeQuery("INSERT INTO TASK_ASSESSMENT (ACTIVE_FLAG, ASSESSMENT_DATE, DESCRIPTION, TITLE, NEEDS_LEVEL_ID, PATIENT_ID, USER_ID) VALUES (":active_flag",":timeStampDate", ":description", ":title", ":needsLevelId", ":patientId", ":userId" )").setParameter("active_flag", your_object.getactive_flag).setParametr and etc
persistableQuery.executeUpdate();
but somewhere create object with all this fields.
Score: 0

In hibernate 5.3 and above positional parameters 3 are deprecated so we need to use keys for 2 parameter. Hql does not support insert with 1 parameter. We need to follow below approch

import org.hibernate.query.Query;


    public void insertData() {

        String sql = "insert into employee(id,name,age,salary) values(:0,:1,:2,:3)";

        List<Object>  paramList = new ArrayList<Object>();
        paramList.add(1);    // id
        paramList.add("sumit"); // name
        paramList.add("23");  // age
        paramList.add(10000);  // salary

        Session session = null;
        try {
            session = getSessionfactory().openSession();

            Query query= session.createNativeQuery(sql);

            for(int i=0;i<paramList.size();i++) {
                query.setParameter(""+i,paramList.get(i));   // remember to add "" before i , we need to maintain key value pair in setParameter()
            }
            query.executeUpdate();
        }
        catch(Exception e) {
            System.out.println(e);
        }

    }

More Related questions