hibernate批处理

nwsw7zdq  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(291)

我在这个链接中尝试了批插入的解决方案hibernate batch insert,但是仍然没有按应该的方式工作。以下是相关代码供您参考。我上传的只是一个试算表文件,其中包括100试算表行。所以一个试算表是100行试算表的列表,我需要批量插入这100行数据。这是一个restapi,am使用json托管引用,因为它是双向关系。
下面代码的结果是在试算表中插入了6行,而试算表行表只有最后一个创建的试算表id插入了100行,因为剩余的id没有在试算表行表中插入数据。
你能指出我哪里出错了吗?

  1. ***TRIAL_BALANCE Entity***
  2. @Entity
  3. @Table(name="TRIAL_BALANCE")
  4. public class TrialBalance {
  5. @Id
  6. @GeneratedValue(strategy=GenerationType.AUTO)
  7. @Column(name="ID")
  8. private int id;
  9. @OneToMany(mappedBy="trialBalance", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
  10. @JsonManagedReference
  11. private List<TrialBalanceRow> trialBalanceRows;
  12. public void addTrialBalanceRows(List<TrialBalanceRow> tbRows) {
  13. if ( trialBalanceRows == null ) {
  14. trialBalanceRows = new ArrayList<TrialBalanceRow>();
  15. }
  16. for ( TrialBalanceRow tb : tbRows ) {
  17. trialBalanceRows.add(tb);
  18. tb.setTrialBalance(this);
  19. }
  20. }
  21. ***TRIAL_BALANCE_ROW Entity***
  22. @Entity
  23. @Table(name="TRIAL_BALANCE_ROW")
  24. public class TrialBalanceRow {
  25. @Id
  26. @GeneratedValue(strategy=GenerationType.AUTO)
  27. @Column(name="ID")
  28. private int id;
  29. @JsonBackReference
  30. @ManyToOne(fetch=FetchType.LAZY)
  31. @JoinColumn(name="TRIAL_BALANCE_ID")
  32. private TrialBalance trialBalance;
  33. ***TrialBalanceDOAImpl.java file***
  34. public void addTrialBalance(TrialBalance trialBalance) {
  35. try {
  36. Session session = sessionFactory.openSession();
  37. Transaction tx = session.beginTransaction();
  38. for ( int i=0; i<1200; i++ ) {
  39. session.save(trialBalance);
  40. if( i % 50 == 0 ) { // Same as the JDBC batch size
  41. //flush a batch of inserts and release memory:
  42. session.flush();
  43. session.clear();
  44. }
  45. }
  46. tx.commit();
  47. session.close();
  48. } catch (HibernateException e) {
  49. e.printStackTrace();
  50. throw new DataNotSavedException();
  51. }
  52. }

休眠属性

  1. hibernate.dialect = org.hibernate.dialect.MySQLDialect
  2. hibernate.show_sql = true
  3. hibernate.format_sql = false
  4. spring.jpa.properties.hibernate.jdbc.time_zone = UTC
  5. spring.jpa.properties.hibernate.jdbc.batch_size = 50
  6. spring.jpa.properties.hibernate.order_inserts=true
  7. spring.jpa.properties.hibernate.order_updates=true
  8. spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true
  9. spring.jpa.properties.hibernate.id.new_generator_mappings = false

休眠配置

  1. private Properties hibernateProperties() {
  2. Properties properties = new Properties();
  3. properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
  4. properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
  5. properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
  6. properties.put("hibernate.jdbc.time_zone",
  7. environment.getRequiredProperty("spring.jpa.properties.hibernate.jdbc.time_zone"));
  8. properties.put("hibernate.jdbc.batch_size",
  9. environment.getRequiredProperty("spring.jpa.properties.hibernate.jdbc.batch_size"));
  10. properties.put("hibernate.order_inserts",
  11. environment.getRequiredProperty("spring.jpa.properties.hibernate.order_inserts"));
  12. properties.put("hibernate.order_updates",
  13. environment.getRequiredProperty("spring.jpa.properties.hibernate.order_updates"));
  14. properties.put("hibernate.jdbc.batch_versioned_data",
  15. environment.getRequiredProperty("spring.jpa.properties.hibernate.jdbc.batch_versioned_data"));
  16. properties.put("hibernate.id.new_generator_mappings",
  17. environment.getRequiredProperty("spring.jpa.properties.hibernate.id.new_generator_mappings"));
  18. return properties;

}
日志
2018-07-12下午16:28:25persister:3142 - 更新实体:[in.greenstack.ikon.entity.trialbalancerow#107399]2018-07-12 16:28:25调试摘要batchimpl:129 - 重用批处理语句2018-07-12 16:28:25调试sql:92 - 更新试算表行设置科目代码=?,科目名称=?,期末余额=?,总账金额币种=?,总账金额属性=?,总账金额修订=?,期初余额=?,备注=?,事务处理cr=?,事务处理dr=?,试算表id=?其中id=?hibernate:更新试算表行设置科目代码=?,科目名称=?,期末余额=?,总账金额币种=?,总账金额属性=?,总账金额修订=?,期初余额=?,备注=?,交易凭证=?,交易凭证=?,试算表id=?其中id=?2018-07-12下午16:28:25persister:2723 - 脱水实体:[in.greenstack.ikon.entity.trialbalancerow#107399]2018-07-12 16:28:25sicbinder:65 - 绑定参数1为[integer]-[0]2018-07-12 16:28:25sicbinder:65 - 绑定参数[2]为[varchar]-[pooja enterprise]2018-07-12 16:28:25sicbinder:65 -绑定参数[3]为[double]-[0.0]2018-07-12 16:28:25sicbinder:53 - 绑定参数[4]为[double]-[null]2018-07-12 16:28:25sicbinder:53 - 绑定参数[5]为[double]-[null]2018-07-12 16:28:25sicbinder:53 - 绑定参数[6]为[double]-[null]2018-07-12 16:28:25sicbinder:65 - 绑定参数[7]为[double]-[0.0]2018-07-12 16:28:25sicbinder:53 - 绑定参数[8]为[varchar]-[null]2018-07-12 16:28:25sicbinder:65 - 绑定参数[9]为[double]-[14000.0]2018-07-12 16:28:25sicbinder:65 - 绑定参数[10]为[double]-[14000.0]2018-07-12 16:28:25sicbinder:65 -绑定参数[11]为[integer]-[361]2018-07-12 16:28:25sicbinder:65 - 绑定参数[12]为[integer]-[107399]2018-07-12 16:28:25跟踪抽象实体persister:3142 - 更新实体:[in.greenstack.ikon.entity.trialbalancerow#107400]2018-07-12 16:28:25调试摘要batchimpl:129 - 重用批处理语句2018-07-1216:28:25调试sql:92 - 更新试算表行设置科目代码=?,科目名称=?,期末余额=?,总账金额币种=?,总账金额属性=?,总账金额修订=?,期初余额=?,备注=?,交易凭证=?,交易凭证=?,试算表id=?其中id=?hibernate:更新试算表行设置科目代码=?,科目名称=?,期末余额=?,总账金额币种=?,总账金额属性=?,总账金额修订=?,期初余额=?,备注=?,交易凭证=?,交易凭证=?,试算表id=?其中id=?2018-07-12下午16:28:25persister:2723 - 脱水实体:[in.greenstack.ikon.entity.trialbalancerow#107400]2018-07-12 16:28:25sicbinder:65 - 绑定参数1为[integer]-[0]2018-07-12 16:28:25sicbinder:65 - 绑定参数[2]as[varchar]-[poonam pride bunglows]2018-07-12 16:28:25跟踪文学士sicbinder:65 - 绑定参数[3]为[double]-[0.0]2018-07-12 16:28:25sicbinder:53 - 绑定参数[4]为[double]-[null]2018-07-12 16:28:25sicbinder:53 - 绑定参数[5]为[double]-[null]2018-07-12 16:28:25sicbinder:53 - 绑定参数[6]为[double]-[null]2018-07-12 16:28:25跟踪文学士sicbinder:65 - 绑定参数[7]为[double]-[3842.0]2018-07-12 16:28:25sicbinder:53 - 绑定参数[8]为[varchar]-[null]2018-07-12 16:28:25sicbinder:65 - 绑定参数[9]为[double]-[15467.0]2018-07-12 16:28:25sicbinder:65 - 绑定参数[10]为[double]-[11625.0]2018-07-12 16:28:25微量钡sicbinder:65 - 绑定参数[11]为[integer]-[361]2018-07-12 16:28:25sicbinder:65 - 绑定参数[12]为[integer]-[107400]2018-07-12 16:28:25调试batchingbatch:384 - 执行批量:50
我正在使用hibernatev5.2.17和springsecurity以及springv5+。
谢谢,巴夫娜

cbjzeqam

cbjzeqam1#

最好的方法是启用jdbc日志记录。
在您的情况下,不清楚您是否正在提交事务。你在用吗 @Transactional 为你效劳?
另外,您应该使用适当的日志框架,而不是 e.printStackTrace .

h9vpoimq

h9vpoimq2#

对于将来需要引用的其他人来说,我的解决方案是为批插入编写自己的sql查询,如下所示。我还删除了父实体中的cascadetype.all特性。

  1. @Override
  2. public void addTrialBalance(TrialBalance trialBalance) {
  3. try {
  4. //Batch insertion code
  5. Session session = sessionFactory.openSession();
  6. Transaction tx = session.beginTransaction();
  7. final int batchSize = 50;
  8. session.save(trialBalance);
  9. session.doWork(new Work()
  10. {
  11. public void execute(Connection con) throws SQLException
  12. {
  13. PreparedStatement st = con.prepareStatement(""
  14. + "insert into TRIAL_BALANCE_ROW (ACCOUNT_CODE, ACCOUNT_NAME, ....,TRIAL_BALANCE_ID) "
  15. + "values (?, ?,...,?)");
  16. List<TrialBalanceRow> tbRows = trialBalance.getTrialBalanceRows();
  17. List<TrialBalanceRow> trialBalanceRows = new ArrayList<TrialBalanceRow>();
  18. int count = 0;
  19. for (TrialBalanceRow tb : tbRows)
  20. {
  21. trialBalanceRows.add(tb);
  22. tb.setTrialBalance(trialBalance);
  23. count++;
  24. st.setString(1, tb.getAccountCode());
  25. st.setString(2, tb.getAccountName());
  26. st.setDouble(3, tb.getClosingBalance());
  27. .
  28. .
  29. .
  30. st.setInt(11, tb.getTrialBalance().getId());
  31. System.out.println("Adding to batch .......");
  32. st.addBatch();
  33. if(count % batchSize == 0) {
  34. System.out.println("Executing Batch Size of : " + batchSize);
  35. st.executeBatch();
  36. }
  37. }
  38. st.executeBatch();
  39. st.close();
  40. }
  41. });
  42. session.refresh(trialBalance);
  43. tx.commit();
  44. session.close();
  45. } catch (HibernateException e) {
  46. e.printStackTrace();
  47. throw new DataNotSavedException();
  48. }
  49. }

感谢vlad提供的参考资料(尤其是generationtype.auto上的参考资料)和所有帮助,但即使使用order\u inserts,我也无法获得所需的结果。通过上述解决方案,我能够达到所需的性能。

展开查看全部

相关问题