2013-12-11 21 views
0

在我们使用DB2数据库的工作中,在一个表中,主键的类型是Numeric。对于那个表我做了一个实体类,并为主键设置了一个BigDecimal属性。当试图在该表格中插入新行时使用将java.math.BigDecimal映射到DB2时出现异常数字数据类型

entityManager.persist(entity); 

一切都正常。新行被添加到表中。但问题是我们需要一次插入多条记录,因此我实现了这种方法:

public void addCars(final List<CarContainer> cars) throws SQLException { 

    Date date = new Date(); 
    final java.sql.Date recDate = new java.sql.Date(date.getTime()); 
    final Timestamp dateCreated = new Timestamp(date.getTime()); 

    Session session = em.unwarp(Session.class); 

    session.doWork(new Work() { 

     @Override 
     public void execute(Connection connection) throws SQLException { 
String carsSql = "INSERT INTO " + 
        "cars" + 
        " (cars_id, cars_name, 
cars_rec_status, created)" + 
        "VALUES" + 
        " (?, ?, ?, ?)"; 

      NullablePreparedStatement carsPreparedStatement = 
NullablePreparedStatement.wrap(connection.prepareStatement(carsSql)); 

      for (CarContainer carContainer : cars) { 

       brandsPreparedStatement 
       .setBigDecimal(1, carContainer.getCarId()) 
       .setString(2, carContainer.getName()) 
       .setShort(3, carContainer.getRecordStatus()) 
       .setDate(4, recDate, 
DateUtil.getCalendarInstance()); 

       carsPreparedStatement.unwrap().addBatch(); 
     } 

carsPreparedStatement.unwrap().executeBatch(); 

      if (!carsPreparedStatement.unwrap().isClosed()) { 
       carsPreparedStatement.unwrap().close(); 
      } 
    }); 
} 

而这就是问题所在。当我使用我得到的例外在该行的代码时,本机查询:

.setBigDecimal(1, carContainer.getBrandsId()) 

出于某种原因,在使用本机插入的BigDecimal没有正确映射到数字。

+0

而例外是......? –

+0

“java.sql.SQLException:Connection is not associated with a managed co[email protected]65cdfb” –

+0

“我没有看到”Connection is not associated ..“ 。“与数据类型映射有关。您是否获得任何DB2 SQLCODE值? – mustaccio

回答

0

@mustaccio谢谢你的提示,但没有必要。我不喝伏特加酒或其他任何东西;)。我的问题解决了,问题出现在使用select查询的方法carContainer.getCarId()中。我不知道在doWork方法中,我不应该做出选择查询。选择EntityManager关闭连接并导致异常后。

相关问题