简体   繁体   中英

How to insert a java ArrayList in postgresql Array (integer[] ) type column using @InsertProvider

I Have a mapper method which is defined as following

@InsertProvider(type = ActivityMapperSQLBuilder.class, method = "insertIntoActivityComment")
    public int insertIntoActivityComment(@Param("activityComment")ActivityComment activityComment);

the corresponding SQLBuilder method is defined as

public String insertIntoActivityComment(Map<String, Object> params) {
        ActivityComment activityComment = (ActivityComment) params
                .get("activityComment");

        params.put("fileIds",  activityComment.getFileIds());
        params.put("commentType",activityComment.getCommentType());
        params.put("commentText",activityComment.getCommentText());
        params.put("commentingUserId",activityComment.getCommentingUser().getId());
        params.put("attachments",activityComment.getAttachments());
        params.put("activityId",activityComment.getActivity().getId());

        StringBuilder builder = new StringBuilder(
                "insert into activityComment (commenttype, commenttext, commentdate, commentinguser_id, attachments, activity_id, fileids) VALUES "
                        + " (#{commentType}, #{commentText}, now(), #{commentingUserId}, #{attachments}, #{activityId}, #{fileIds} )");

        return builder.toString();
    }

whenever I call my mappermethod as following

getActivityMapper().insertIntoActivityComment(activityComment);

I encountered following error.

error updating database.  Cause: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.ArrayList. Use setObject() with an explicit Types value to specify the type to use.

The error may involve ActivityMapper.insertIntoActivityComment-Inline The error occurred while setting parameters\\n### SQL: insert into activityComment (commenttype, commenttext, commentdate, commentinguser_id, attachments, activity_id, fileids) VALUES (?, ?, now(), ?, ?, ?, ? )\\n### Cause: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.ArrayList. Use setObject() with an explicit Types value to specify the type to use."

the structure of my ActivityComment Object is

public class ActivityComment implements Serializable, IsSerializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private ActivityCommentType commentType;
    private String commentText;
    private Timestamp commentDate;
    private Assignment activity;
    private User commentingUser;
    private String attachments;
    private List<Integer> fileIds = new ArrayList<Integer>();

/*getters and setters*
}

Can anyone help me inserting ArrayList into the postgresql database?

What you will have to do is, get the data to be inserted. Then you will have to add them in an arrayList. Then am assuming you will be passing the arrayList to a function that does the job of inserting the data into the DB.

To insert the arraylist u need to frame the query dynamically.

I will write an example code for you, do some changes if you need to.

//Adding data into an ArrayList

ArrayList<String> data=new ArrayList<String>();
data.add(value1);
data.add(value2);
data.add(value3);
data.add(value4);
insertIntoMyDatabase(data); // function that will take this arraylist form a query and do the insertion.

//insertIntoMyDatabase :

public void insertIntoMyDatabase(ArrayList<String> data) {
PreparedStatement p;
String newdata="";
  for(i=0;i<data.size();i++){ // This loop will take care of framing the query dynamically

                if(i==data.size()-1){
                    newdata+="'"+data.toArray()[i]+"'";
                }else{
                    newdata+="'"+data.toArray()[i]+"',";
                }
String query="insert into table values("+newdata+")"; // This is the framed query for inserting data
try{
p=con.prepareStatement(query);
ResultSet r=pr.executeQuery();
}catch(SQLExecption e){
e.printStacktrace();
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM