我在我的 DAO 类中使用以下事务代码单元在 Hibernate 中插入多行。
@Override
@SuppressWarnings("unchecked")
@Transactional(readOnly = false, propagation=Propagation.REQUIRES_NEW, rollbackFor={Throwable.class})
public String insertZoneCharge(Long zoneId, List<Object[]> items)
{
Session session=sessionFactory.openSession();
session.beginTransaction();
//Configuration configuration=new Configuration();
//configuration.setProperty("hibernate.jdbc.batch_size", "50");
int i=1;
for(Object[] o:items)
{
if(o[2]!=null&&StringUtils.isNotBlank(o[2].toString()))
{
ZoneChargePK zoneChargeId=new ZoneChargePK();
zoneChargeId.setWeightId(Long.parseLong(o[0].toString()));
zoneChargeId.setZoneId(zoneId);
model.ZoneCharge zoneCharge=new model.ZoneCharge();
zoneCharge.setZoneChargePK(zoneChargeId);
zoneCharge.setCharge(new BigDecimal(o[2].toString()).setScale(2, RoundingMode.HALF_UP));
session.save(zoneCharge);
if(++i%50==0)
{
session.flush();
session.clear();
}
}
}
session.getTransaction().commit();
session.close();
return "Data saved successfully.";
}
此方法的参数是通过 JSON 请求提供的。我不会深入讨论 Oracle 表的详细信息,因为它是无关的。
当我提供 3 个值时,这个循环会迭代三次,并在相应的数据库表中创建三行。事务成功完成后,INSERT
我可以在控制台上看到的以下本机 Oracle 语句似乎被执行了三次。
insert into WAGAFASHIONDB.ZONE_CHARGE (CHARGE, WEIGHT_ID, ZONE_ID) values (?, ?, ?)
调试和跟踪信息如下。
DEBUG [http-apr-8080-exec-84] (SqlStatementLogger.java:104) - insert into WAGAFASHIONDB.ZONE_CHARGE (CHARGE, WEIGHT_ID, ZONE_ID) values (?, ?, ?)
TRACE [http-apr-8080-exec-84] (BasicBinder.java:83) - binding parameter [1] as [NUMERIC] - 1.00
TRACE [http-apr-8080-exec-84] (BasicBinder.java:83) - binding parameter [2] as [BIGINT] - 164
TRACE [http-apr-8080-exec-84] (BasicBinder.java:83) - binding parameter [3] as [BIGINT] - 22
DEBUG [http-apr-8080-exec-84] (SqlStatementLogger.java:104) - insert into WAGAFASHIONDB.ZONE_CHARGE (CHARGE, WEIGHT_ID, ZONE_ID) values (?, ?, ?)
TRACE [http-apr-8080-exec-84] (BasicBinder.java:83) - binding parameter [1] as [NUMERIC] - 2.00
TRACE [http-apr-8080-exec-84] (BasicBinder.java:83) - binding parameter [2] as [BIGINT] - 221
TRACE [http-apr-8080-exec-84] (BasicBinder.java:83) - binding parameter [3] as [BIGINT] - 22
DEBUG [http-apr-8080-exec-84] (SqlStatementLogger.java:104) - insert into WAGAFASHIONDB.ZONE_CHARGE (CHARGE, WEIGHT_ID, ZONE_ID) values (?, ?, ?)
TRACE [http-apr-8080-exec-84] (BasicBinder.java:83) - binding parameter [1] as [NUMERIC] - 3.00
TRACE [http-apr-8080-exec-84] (BasicBinder.java:83) - binding parameter [2] as [BIGINT] - 163
TRACE [http-apr-8080-exec-84] (BasicBinder.java:83) - binding parameter [3] as [BIGINT] - 22
因此,似乎INSERT
已经执行了三个单独的语句,并且已经对数据库进行了三个单独的访问。
我希望它在一次行程中只执行一个多行INSERT
语句。在这种情况下,Hibernate 真的会执行三个单独的语句吗?如果是,那么在特定批次中执行语句的方式是什么?我的代码有什么问题吗?
我的 Hibernate 配置application-context.xml
如下。
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.jdbc.batch_size=50
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.format_sql=false
hibernate.show_sql=true
hibernate.order_updates=true
hibernate.connection.autocommit=false
hibernate.order_inserts=true
hibernate.order_updates=true
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=false
javax.persistence.validation.mode=callback
</value>
</property>