0

次のように定義されたマッパーメソッドがあります

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

対応する SQLBuilder メソッドは次のように定義されます。

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();
    }

次のようにマッパーメソッドを呼び出すたびに

getActivityMapper().insertIntoActivityComment(activityComment);

次のエラーが発生しました。

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.

エラーには、ActivityMapper.insertIntoActivityComment-Inline が関係している可能性があります パラメータの設定中にエラーが発生しました\n### SQL: insert into activityComment (commenttype, commenttext, commentdate, commentinguser_id, attachments, activity_id, fileids) VALUES (?, ?, now(), ?, ?, ?, ? )\n### 原因: org.postgresql.util.PSQLException: java.util.ArrayList のインスタンスに使用する SQL タイプを推測できません。使用するタイプを指定するには、setObject() を明示的な Types 値とともに使用します。"

私のActivityCommentオブジェクトの構造は

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*
}

PostgresqlデータベースにArrayListを挿入するのを手伝ってくれる人はいますか?

4

1 に答える 1

0

あなたがしなければならないことは、挿入するデータを取得することです。次に、それらをarrayListに追加する必要があります。次に、データを DB に挿入するジョブを実行する関数に arrayList を渡すと想定しています。

配列リストを挿入するには、クエリを動的に組み立てる必要があります。

サンプルコードを書きますので、必要に応じて変更してください。

// 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();
}

}
于 2014-09-08T12:55:32.333 に答える