`
kanpiaoxue
  • 浏览: 1781839 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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.

shareimprove this question
 

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

shareimprove this answer
 
    
Hi cannot i insert more records being on the same page, on first insertion data is getting inserted , but after navigating and again coming to insertion page , this kind of error iam getting. could u please tell is thier any configureation required to insert more records – Renukeswar Mar 30 '13 at 2:33
    
are you inserting the the data into same table after page page navigation? Please attache your code to let people reproduce the issue and figure out where goes wrong. – spiritwalker Mar 30 '13 at 2:57
    
@Resource(name="dataSource") public void setSimpleJdbcTemplate(DataSource dataSource) { this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); and other paramertes set into Map, simpleJdbcInsert.execute(parameterMap). this.simpleJdbcInsert = new SimpleJdbcInsert(dataSource); } code to insert into DB: this.simpleJdbcInsert.withTableName("MOVIE_INFO"); Map<String,Object> parameterMap = new HashMap<String,Object>(); parameterMap.put("MOVIE_NAME", movie.getMovieName()); – Renukeswar Mar 30 '13 at 4:53 
    
did any one come across this kind of issue. I saw AbstractJdbcInsert ,in which isCompile method checks for this is compile variable is set true this exception is thrown – Renukeswar Mar 30 '13 at 10:33
    
remove ** this.simpleJdbcInsert.withTableName("MOVIE_INFO");**. withTableName does nothing rather than just setting table name. And please refer to my answer, you cannot set table name multiple times to same SimpleJdbcInsert instance. – spiritwalker Mar 30 '13 at 11:47 

 

 

 

分享到:
评论

相关推荐

    org.springframework.dao.InvalidDataAccessApiUsageException

    其中一种较为常见的异常是 `org.springframework.dao.InvalidDataAccessApiUsageException`。这类异常通常发生在使用Spring框架进行数据库操作(如增、删、改)时,特别是在采用Spring的注解式事务管理机制时。 ###...

    Spring的DataAccessException

    7. InvalidDataAccessApiUsageException:一个数据访问的 JAVA API 没有正确使用,例如必须在执行前编译好的查询编译失败了。 8. invalidDataAccessResourceUsageException:错误使用数据访问资源,例如用错误的 SQL...

    HibernateTemplate源代码

    public &lt;T&gt; T execute(HibernateCallback&lt;T&gt; action) throws DataAccessException { Assert.notNull(action, "Callback object must not be null"); SessionFactory sessionFactory = getSessionFactory(); if ...

    hibernate错误解决方案

    org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker ...

    Open_Session_In_View详解.doc

    org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker ...

    hibernate中文教程参考文档

    Hibernate的异常体系基于JDBC异常,主要包括PersistenceException、ConstraintViolationException、InvalidDataAccessApiUsageException等,理解这些异常有助于调试和优化代码。 **10. 性能优化** 优化Hibernate...

    Spring boot jpa 删除数据和事务管理的问题实例详解

    问题 1:如果各层都不加事务管理的话,会报错 `org.springframework.dao.InvalidDataAccessApiUsageException`。 解决方案:在各层中添加 `@Transactional` 注解,例如: ```java @Transactional public void ...

    Hibernate事务管理.

    这时,可能会遇到`InvalidDataAccessApiUsageException`异常,提示写操作不允许在已开启的事务之外进行。 为了解决这个问题,通常建议在Service层使用Spring的`@Transactional`注解来管理事务,这样可以更精确地...

    SPRING API 2.0.CHM

    InvalidDataAccessApiUsageException InvalidDataAccessResourceUsageException InvalidDestinationException InvalidInvocationException InvalidIsolationLevelException InvalidMetadataException ...

Global site tag (gtag.js) - Google Analytics