问题描述:实体类直接调用save方法的时候报错“com.jfinal.plugin.activerecord.ActiveRecordException: java.sql.SQLSyntaxErrorException: ORA-00942: 表或视图不存在at com.jfinal.plugin.activerecord.Model.save(Model.java:562).......”同时查询和修改没有问题,就只有插入报错。
开发环境:oracle12c jfinal4.8
问题分析:
是因为com.jfinal.plugin.activerecord.Model类里面save方法有如下判斷:
if (config.dialect.isOracle()) {
pst = conn.prepareStatement(sql.toString(), table.getPrimaryKey());
} else {
pst = conn.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
}其中报错的是这里:
pst = conn.prepareStatement(sql.toString(), table.getPrimaryKey());
这里涉及到jdbc的基础知识问题,我在此不想进行更深入的讨论。直接给解决办法:
1新建MyModel类,重写save方法。
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.jfinal.plugin.activerecord.ActiveRecordException;
import com.jfinal.plugin.activerecord.Config;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.dialect.Dialect;
public class MyModel<M extends MyModel<M>> extends Model<M> {
public boolean save() {
filter(FILTER_BY_SAVE);
Dialect dialect = _getConfig().getDialect();
Config config = _getConfig();
Table table = _getTable();
StringBuilder sql = new StringBuilder();
List<Object> paras = new ArrayList<Object>();
dialect.forModelSave(table, _getAttrs(), sql, paras);
Connection conn = null;
PreparedStatement pst = null;
int result = 0;
try {
conn = config.getConnection();
pst = conn.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
dialect.fillStatement(pst, paras);
result = pst.executeUpdate();
dialect.getModelGeneratedKey(this, pst, table);
_getModifyFlag().clear();
return result >= 1;
} catch (Exception e) {
throw new ActiveRecordException(e);
} finally {
config.close(pst, conn);
}
}
}2 让你的实体基类继承MyModel
public abstract class BaseXjrBaseLog<M extends BaseXjrBaseLog<M>> extends MyModel<M> implements IBean
解决问题。
感谢Jfinal官方老师的支持!
生成 BaseXjrBaseLog 时需要用到一个你定制的 base_model_template.jf