java - Hibernate批量插入數(shù)據(jù)總是插入不完整
問(wèn)題描述
在利用hibernate向數(shù)據(jù)庫(kù)插入數(shù)據(jù)的時(shí)候發(fā)現(xiàn)總共2000多條數(shù)據(jù)只能插入一部分到數(shù)據(jù)庫(kù)中,debug的時(shí)候發(fā)現(xiàn)在循環(huán)中確實(shí)是建立了對(duì)象并且調(diào)用了save()方法的我的代碼如下
Configuration configuration = new Configuration().configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String file = 'src/1.xlsx'; InputStream is = null; try {is = new FileInputStream(file); } catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace(); } XSSFWorkbook xssfWorkbook = null; try {xssfWorkbook = new XSSFWorkbook(is); } catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace(); } // 獲取每一個(gè)工作薄 for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);if (xssfSheet == null) { continue;}// 獲取當(dāng)前工作薄的每一行for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) { XSSFRow xssfRow = xssfSheet.getRow(rowNum); if (xssfRow != null) {XSSFCell one = xssfRow.getCell(0);XSSFCell two = xssfRow.getCell(1);XSSFCell three = xssfRow.getCell(2);String name = getValue(three);String code;if (getValue(one).equals('滬市')) code = 'sh' + getValue(two);else code = 'sz' + getValue(two);StockName2Code s = new StockName2Code();s.setCode(code);s.setName(name);session.save(s);if (rowNum % 20 == 0) { session.flush(); session.clear();} }} } tx.commit(); session.close(); sessionFactory.close();
我的配置文件如下
` <!DOCTYPE hibernate-configuration PUBLIC
'-//Hibernate/Hibernate Configuration DTD 3.0//EN' 'http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd'>
<hibernate-configuration>
<session-factory> <!-- Database connection settings --> <!-- 表示使用 mysql 數(shù)據(jù)庫(kù)驅(qū)動(dòng)類 --> <property name='connection.driver_class'>com.mysql.jdbc.Driver</property> <!-- jdbc 的連接 url 和數(shù)據(jù)庫(kù) --> <property name='connection.url'>jdbc:mysql://*******/******?useUnicode=true&characterEncoding=UTF-8</property> <!-- 數(shù)據(jù)庫(kù)用戶名 --> <property name='connection.username'>root</property> <!-- 密碼(這里為空) --> <property name='connection.password'>********</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name='connection.pool_size'>1</property> --> <!-- 數(shù)據(jù)庫(kù)使用的方言 --> <property name='dialect'>org.hibernate.dialect.MySQL5Dialect</property> <!-- Echo all executed SQL to stdout --> <!-- 設(shè)置 打印輸出 sql 語(yǔ)句 為真 --> <property name='show_sql'>true</property> <!-- 設(shè)置格式為 sql --> <property name='format_sql'>true</property> <!-- 第一次加載 hibernate 時(shí)根據(jù)實(shí)體類自動(dòng)建立表結(jié)構(gòu),以后自動(dòng)更新表結(jié)構(gòu) --> <property name='hbm2ddl.auto'>update</property> <!-- 映射文件 --> <mapping /></session-factory>
</hibernate-configuration> `
雖然并沒(méi)有拋出內(nèi)存用完的異常,但是因?yàn)樵谒阉鞯臅r(shí)候發(fā)現(xiàn)可能是由于hibernate緩存的問(wèn)題,所以加上了每20條強(qiáng)制刷新的代碼塊,但是最后發(fā)現(xiàn)還是沒(méi)有效果,插入操作還是只能進(jìn)行一部分,不知道還有沒(méi)有什么可能的原因?
問(wèn)題解答
回答1:hibernate已經(jīng)設(shè)置為show_sql了,打印出來(lái)sql數(shù)量跟excel數(shù)據(jù)數(shù)量一致嗎
相關(guān)文章:
1. php - 淘寶訂單拆單表設(shè)計(jì)2. 實(shí)現(xiàn)bing搜索工具urlAPI提交3. 如何用筆記本上的apache做微信開(kāi)發(fā)的服務(wù)器4. mysql優(yōu)化 - MySQL如何為配置表建立索引?5. 冒昧問(wèn)一下,我這php代碼哪里出錯(cuò)了???6. MySQL主鍵沖突時(shí)的更新操作和替換操作在功能上有什么差別(如圖)7. 關(guān)于mysql聯(lián)合查詢一對(duì)多的顯示結(jié)果問(wèn)題8. 數(shù)據(jù)庫(kù) - Mysql的存儲(chǔ)過(guò)程真的是個(gè)坑!求助下面的存儲(chǔ)過(guò)程哪里錯(cuò)啦,實(shí)在是找不到哪里的問(wèn)題了。9. 我在網(wǎng)址中輸入localhost/abc.php顯示的是not found是為什么呢?10. windows誤人子弟啊
