java - Hibernate批量插入數(shù)據(jù)總是插入不完整
問題描述
在利用hibernate向數(shù)據(jù)庫插入數(shù)據(jù)的時候發(fā)現(xiàn)總共2000多條數(shù)據(jù)只能插入一部分到數(shù)據(jù)庫中,debug的時候發(fā)現(xiàn)在循環(huán)中確實是建立了對象并且調(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(); } // 獲取每一個工作薄 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ù)庫驅(qū)動類 --> <property name='connection.driver_class'>com.mysql.jdbc.Driver</property> <!-- jdbc 的連接 url 和數(shù)據(jù)庫 --> <property name='connection.url'>jdbc:mysql://*******/******?useUnicode=true&characterEncoding=UTF-8</property> <!-- 數(shù)據(jù)庫用戶名 --> <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ù)庫使用的方言 --> <property name='dialect'>org.hibernate.dialect.MySQL5Dialect</property> <!-- Echo all executed SQL to stdout --> <!-- 設(shè)置 打印輸出 sql 語句 為真 --> <property name='show_sql'>true</property> <!-- 設(shè)置格式為 sql --> <property name='format_sql'>true</property> <!-- 第一次加載 hibernate 時根據(jù)實體類自動建立表結(jié)構(gòu),以后自動更新表結(jié)構(gòu) --> <property name='hbm2ddl.auto'>update</property> <!-- 映射文件 --> <mapping /></session-factory>
</hibernate-configuration> `
雖然并沒有拋出內(nèi)存用完的異常,但是因為在搜索的時候發(fā)現(xiàn)可能是由于hibernate緩存的問題,所以加上了每20條強(qiáng)制刷新的代碼塊,但是最后發(fā)現(xiàn)還是沒有效果,插入操作還是只能進(jìn)行一部分,不知道還有沒有什么可能的原因?
問題解答
回答1:hibernate已經(jīng)設(shè)置為show_sql了,打印出來sql數(shù)量跟excel數(shù)據(jù)數(shù)量一致嗎
相關(guān)文章:
1. angular.js - angular內(nèi)容過長展開收起效果2. 關(guān)于nginx location配置的問題,root到底是什么3. 關(guān)于docker下的nginx壓力測試4. angular.js - angularjs的自定義過濾器如何給文字加顏色?5. docker鏡像push報錯6. python - flask表單 如何把提交多行數(shù)據(jù)在服務(wù)端讀取出來?7. python 怎樣用pickle保存類的實例?8. 并發(fā)模型 - python將進(jìn)程池放在裝飾器里為什么不生效也沒報錯9. python的前景到底有大?如果不考慮數(shù)據(jù)挖掘,機(jī)器學(xué)習(xí)這塊?10. 大家好,請問在python腳本中怎么用virtualenv激活指定的環(huán)境?
