我在这个链接中尝试了批插入的解决方案hibernate batch insert,但是仍然没有按应该的方式工作。以下是相关代码供您参考。我上传的只是一个试算表文件,其中包括100试算表行。所以一个试算表是100行试算表的列表,我需要批量插入这100行数据。这是一个restapi,am使用json托管引用,因为它是双向关系。
下面代码的结果是在试算表中插入了6行,而试算表行表只有最后一个创建的试算表id插入了100行,因为剩余的id没有在试算表行表中插入数据。
你能指出我哪里出错了吗?
***TRIAL_BALANCE Entity***
@Entity
@Table(name="TRIAL_BALANCE")
public class TrialBalance {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")
private int id;
@OneToMany(mappedBy="trialBalance", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@JsonManagedReference
private List<TrialBalanceRow> trialBalanceRows;
public void addTrialBalanceRows(List<TrialBalanceRow> tbRows) {
if ( trialBalanceRows == null ) {
trialBalanceRows = new ArrayList<TrialBalanceRow>();
}
for ( TrialBalanceRow tb : tbRows ) {
trialBalanceRows.add(tb);
tb.setTrialBalance(this);
}
}
***TRIAL_BALANCE_ROW Entity***
@Entity
@Table(name="TRIAL_BALANCE_ROW")
public class TrialBalanceRow {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")
private int id;
@JsonBackReference
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="TRIAL_BALANCE_ID")
private TrialBalance trialBalance;
***TrialBalanceDOAImpl.java file***
public void addTrialBalance(TrialBalance trialBalance) {
try {
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<1200; i++ ) {
session.save(trialBalance);
if( i % 50 == 0 ) { // Same as the JDBC batch size
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
throw new DataNotSavedException();
}
}
休眠属性
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = false
spring.jpa.properties.hibernate.jdbc.time_zone = UTC
spring.jpa.properties.hibernate.jdbc.batch_size = 50
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true
spring.jpa.properties.hibernate.id.new_generator_mappings = false
休眠配置
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
properties.put("hibernate.jdbc.time_zone",
environment.getRequiredProperty("spring.jpa.properties.hibernate.jdbc.time_zone"));
properties.put("hibernate.jdbc.batch_size",
environment.getRequiredProperty("spring.jpa.properties.hibernate.jdbc.batch_size"));
properties.put("hibernate.order_inserts",
environment.getRequiredProperty("spring.jpa.properties.hibernate.order_inserts"));
properties.put("hibernate.order_updates",
environment.getRequiredProperty("spring.jpa.properties.hibernate.order_updates"));
properties.put("hibernate.jdbc.batch_versioned_data",
environment.getRequiredProperty("spring.jpa.properties.hibernate.jdbc.batch_versioned_data"));
properties.put("hibernate.id.new_generator_mappings",
environment.getRequiredProperty("spring.jpa.properties.hibernate.id.new_generator_mappings"));
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+。
谢谢,巴夫娜
2条答案
按热度按时间cbjzeqam1#
最好的方法是启用jdbc日志记录。
在您的情况下,不清楚您是否正在提交事务。你在用吗
@Transactional
为你效劳?另外,您应该使用适当的日志框架,而不是
e.printStackTrace
.h9vpoimq2#
对于将来需要引用的其他人来说,我的解决方案是为批插入编写自己的sql查询,如下所示。我还删除了父实体中的cascadetype.all特性。
感谢vlad提供的参考资料(尤其是generationtype.auto上的参考资料)和所有帮助,但即使使用order\u inserts,我也无法获得所需的结果。通过上述解决方案,我能够达到所需的性能。