Apabila menggunakan hibernate untuk memasukkan data ke dalam pangkalan data, saya mendapati bahawa hanya sebahagian daripada jumlah lebih daripada 2,000 keping data boleh dimasukkan ke dalam pangkalan data Apabila menyahpepijat, saya mendapati bahawa objek itu memang dicipta dalam gelung dan save()
kaedah dipanggil
Kod saya adalah seperti berikut
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 block
e.printStackTrace();
}
XSSFWorkbook xssfWorkbook = null;
try {
xssfWorkbook = new XSSFWorkbook(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.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();
Fail konfigurasi saya adalah seperti berikut
` <!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<konfigurasi hibernate>
<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 語句 為真 -->
<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 class="pojo.StockName2Code" />
</session-factory>
</hibernate-configuration>
Walaupun tiada pengecualian kehabisan memori dilemparkan, semasa mencari, saya mendapati ia mungkin disebabkan oleh masalah cache hibernate, jadi saya menambah blok kod yang memaksa muat semula setiap 20 item, tetapi akhirnya saya mendapati ia masih tidak mempunyai kesan, dan operasi sisipan masih saya hanya boleh lakukan sebahagian daripadanya, adakah ada sebab yang mungkin?
hibernate telah ditetapkan kepada show_sql, adakah bilangan sql yang dicetak konsisten dengan bilangan data excel?