java - Hibernate批量插入数据总是插入不完整
问题描述
在利用hibernate向数据库插入数据的时候发现总共2000多条数据只能插入一部分到数据库中,debug的时候发现在循环中确实是建立了对象并且调用了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;}// 获取当前工作薄的每一行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 数据库驱动类 --> <property name='connection.driver_class'>com.mysql.jdbc.Driver</property> <!-- jdbc 的连接 url 和数据库 --> <property name='connection.url'>jdbc:mysql://*******/******?useUnicode=true&characterEncoding=UTF-8</property> <!-- 数据库用户名 --> <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> --> <!-- 数据库使用的方言 --> <property name='dialect'>org.hibernate.dialect.MySQL5Dialect</property> <!-- Echo all executed SQL to stdout --> <!-- 设置 打印输出 sql 语句 为真 --> <property name='show_sql'>true</property> <!-- 设置格式为 sql --> <property name='format_sql'>true</property> <!-- 第一次加载 hibernate 时根据实体类自动建立表结构,以后自动更新表结构 --> <property name='hbm2ddl.auto'>update</property> <!-- 映射文件 --> <mapping /></session-factory>
</hibernate-configuration> `
虽然并没有抛出内存用完的异常,但是因为在搜索的时候发现可能是由于hibernate缓存的问题,所以加上了每20条强制刷新的代码块,但是最后发现还是没有效果,插入操作还是只能进行一部分,不知道还有没有什么可能的原因?
问题解答
回答1:hibernate已经设置为show_sql了,打印出来sql数量跟excel数据数量一致吗
相关文章:
1. docker网络端口映射,没有方便点的操作方法么?2. 为什么我ping不通我的docker容器呢???3. angular.js - angular内容过长展开收起效果4. docker不显示端口映射呢?5. nignx - docker内nginx 80端口被占用6. docker绑定了nginx端口 外部访问不到7. dockerfile - 我用docker build的时候出现下边问题 麻烦帮我看一下8. docker api 开发的端口怎么获取?9. debian - docker依赖的aufs-tools源码哪里可以找到啊?10. docker - 各位电脑上有多少个容器啊?容器一多,自己都搞混了,咋办呢?
