我试图了解不同的休眠FlushMode之间的区别。为此,我创建了一个小的插入示例。
session.beginTransaction();
session.setFlushMode(FlushMode.AUTO);// I tried ALWAYS AND MANUAL
for (int i = 1; i < 4; i++) {
Stock stock = new Stock();
stock.setStockId(i);
stock.setStockCode("code-"+i);
stock.setStockName("name-"+i);
session.save(stock);
System.out.println("entity saved");
}
session.getTransaction().commit();
我的问题始终与将flushMode设置为COMMIT时的行为相同。
entity saved
entity saved
entity saved
insert into stock (STOCK_CODE, STOCK_NAME, STOCK_ID) values (?, ?, ?)
insert into stock (STOCK_CODE, STOCK_NAME, STOCK_ID) values (?, ?, ?)
insert into stock (STOCK_CODE, STOCK_NAME, STOCK_ID) values (?, ?, ?)
当我期望有
entity saved
insert into stock (STOCK_CODE, STOCK_NAME, STOCK_ID) values (?, ?, ?)
entity saved
insert into stock (STOCK_CODE, STOCK_NAME, STOCK_ID) values (?, ?, ?)
entity saved
insert into stock (STOCK_CODE, STOCK_NAME, STOCK_ID) values (?, ?, ?)
有人解释了为什么我总是有相同的结果吗?
最佳答案
FlushMode.COMMIT
的文档:
调用Transaction#commit时会刷新会话。
如果在循环内提交事务,则结果应为您期望的结果。
问候
慢
关于java - 刷新模式 hibernate 实用程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27313964/