例如:oracle表名:t_user pk:userid 自增序列名:seq_user_id 对应model:User 字段有:userid username List userList = new ArrayList(); userList.add(User.DAO.set("userid","seq_user_id.nextval").set("username","jfinal")); userList.add(User.DAO.set("userid","seq_user_id.nextval").set("username","redis")); Db.batchSave(userList ,2); //////////////////////// public class User extends Model { private static final long serialVersionUID = 4827606126715970198L; public static final User DAO = new User(); }
报错: INFO - Sql: insert into t_user(username, userid) values(?, seq_user_id.nextval) ERROR - com.jfinal.plugin.activerecord.ActiveRecordException: java.sql.SQLException: 无效的列索引 at com.jfinal.plugin.activerecord.DbPro.batch(DbPro.java:981) at com.jfinal.plugin.activerecord.DbPro.batchSave(DbPro.java:1068) at com.jfinal.plugin.activerecord.Db.batchSave(Db.java:546)
@jfinal 3.0版,这个问题还是没改。这个框架没有做单元测试吧。把DbPro.java中的两处: int index = 0; // the same as the iterator in Dialect.forModelSave() to ensure the order of the attrs for (Entry e: attrs.entrySet()) attrNames[index++] = e.getKey(); String columns = StrKit.join(attrNames, ","); 代码改成: StringBuffer columns = new StringBuffer(); for (Entry e : attrs.entrySet()){ String key = e.getKey(); if (attrs.get(key) instanceof String && config.dialect.isOracle() && ((String)attrs.get(key)).endsWith(".nextval")) { } else { columns.append(key+","); } } if (columns.length()>1) { columns = columns.deleteCharAt(columns.length()-1); }
@JFinal 你好 public int[] batchSave(String tableName, List recordList, int batchSize) oracle 自增主键时这个方法还是不能用。 3.6版的也是。 这个方法中把主键从columns删除的同时,应该把主键赋值给pKeysNoUse
修正后的方法: public int[] batchSave(String tableName, List recordList, int batchSize) { if (recordList == null || recordList.size() == 0) return new int[0];
Record record = recordList.get(0); Map cols = record.getColumns(); int index = 0; StringBuilder columns = new StringBuilder(); String pKeys = "";//增加的 // the same as the iterator in Dialect.forDbSave() to ensure the order of the columns for (Entry e : cols.entrySet()) { if (config.dialect.isOracle()) { // 支持 oracle 自增主键 Object value = e.getValue(); if (value instanceof String && ((String)value).endsWith(".nextval")) { pKeys = pKeys.concat(",").concat(e.getKey());//增加的 continue ; } }
if (index++ > 0) { columns.append(','); } columns.append(e.getKey()); } if(pKeys.startsWith(",")){ pKeys.replaceFirst(",", ""); } StringBuilder sql = new StringBuilder(); String[] pKeysNoUse = pKeys.split(",");//修改的 List