SimpleJdbcInsert错误:InvalidDataAccessApiUsageException: Configuration can't
今天使用 org.springframework.jdbc.core.simple.SimpleJdbcInsert来进行一些操作,代码如下:
@Repository("auditLogDao") public class AuditLogDaoImpl implements AuditLogDao { private static final Logger LOGGER = LoggerFactory.getLogger(AuditLogDaoImpl.class); @Autowired private SimpleJdbcInsert simpleJdbcInsert; /* * (non-Javadoc) */ @Override public void add(EntityOperationAuditLog entityOperationAuditLog) { Preconditions.checkNotNull(entityOperationAuditLog); if (null == entityOperationAuditLog.getGmtCreate()) { entityOperationAuditLog.setGmtCreate(new Date()); } LOGGER.debug("start insert tb_operation_audit_log {} ", entityOperationAuditLog); // 表名 simpleJdbcInsert.withTableName("tb_operation_audit_log"); SqlParameterSource parameters = new BeanPropertySqlParameterSource(entityOperationAuditLog); // 插入db并返回自增主键id simpleJdbcInsert.execute(parameters); } }
该代码执行第一次的时候是很好的,可是执行第二次的时候就报错了,如下:
org.springframework.dao.InvalidDataAccessApiUsageException: Configuration can't be altered once the class has been compiled or used
然后去查了查,发现这个SimpleJdbcInsert只能指定一次tableName,不能为同一个SimpleJdbcInsert的实例多次设置tableName。所以才引起上面的异常信息。
改为如下的代码就解决了问题:
@Repository("auditLogDao") public class AuditLogDaoImpl implements AuditLogDao { private static final Logger LOGGER = LoggerFactory.getLogger(AuditLogDaoImpl.class); private SimpleJdbcInsert simpleJdbcInsert; public AuditLogDaoImpl() { super(); } @Autowired public AuditLogDaoImpl(SimpleJdbcInsert simpleJdbcInsert) { super(); this.simpleJdbcInsert = simpleJdbcInsert; simpleJdbcInsert.withTableName("tb_operation_audit_log"); } /* * (non-Javadoc) * @see * EntityOperationAuditLog) */ @Override public void add(EntityOperationAuditLog entityOperationAuditLog) { Preconditions.checkNotNull(entityOperationAuditLog); if (null == entityOperationAuditLog.getGmtCreate()) { entityOperationAuditLog.setGmtCreate(new Date()); } LOGGER.debug("start insert tb_operation_audit_log {} ", entityOperationAuditLog); // 表名 // simpleJdbcInsert.withTableName("tb_operation_audit_log"); SqlParameterSource parameters = new BeanPropertySqlParameterSource(entityOperationAuditLog); // 插入db并返回自增主键id simpleJdbcInsert.execute(parameters); } }
参考: http://stackoverflow.com/questions/15606588/org-springframework-dao-invaliddataaccessapiusageexception-configuration-cant
Iam inserting some data into DB with simpleJdbcInsert in spring , it works fine for first step (i mean for first insertion ) , when i try yo save the data for second time iam getting exception as :org.springframework.dao.InvalidDataAccessApiUsageException: Configuration can't be altered once the class has been compiled or used." Can any one help me out in this. spring-mvc
|
||
add a comment
|
1 Answer
up vote1down vote
|
This exception usually happens when you try to config(again) a compiled simpleJdbcInsert. compiled means you have instantiated a simpleJdbcInsert instance and set up data source andtable name already. Once an simpleJdbcInsert instance is compiled, you should not re-config it again; for instance, set another table name. Create a new simpleJdbcInsert instace if you need to do so. To get a comprehensive understanding about how simpleJdbcInsert works, take a look into source code of simpleJdbcInsert and AbstractJdbcInsert. especially the method compile() inAbstractJdbcInsert.java |
||||||||||||||||||||
show 4 more comments
|