我是Spring批处理的新手。我试图使用HibernateItemWriter
写入目标数据库。我在执行批处理作业时遇到异常。
以下是我的申请的例外和代码
HibernateItemWriter
配置: -
@StepScope
@Bean
public ItemWriter<NewEmployee> hibernateEmployeeItemWriter() throws IOException {
HibernateItemWriter<NewEmployee> itemWriter = new HibernateItemWriter<>();
itemWriter.setSessionFactory(sessionFactory().getObject());
itemWriter.setClearSession(true);
return itemWriter;
}
会话工厂和事务管理器代码
@Bean
public LocalSessionFactoryBean sessionFactory() throws IOException{
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
factoryBean.setDataSource(configuration.getDataSource());
// factoryBean.setAnnotatedPackages("com.qiwkreport.qiwk.etl.domain");
factoryBean.setPackagesToScan("com.qiwkreport.qiwk.etl.domain");
factoryBean.afterPropertiesSet();
return factoryBean;
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
return jpaTransactionManager;
}
实体类代码: -
Entity
@Table(name = "NEWEMPLOYEE")
public class NewEmployee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private long id;
@Column(name = "FIRSTNAME")
private String firstName;
@Column(name = "LASTNAME")
private String lastName;
@Column(name = "VILLAGE")
private String village;
@Column(name = "STREET")
private String street;
@Column(name = "CITY")
private String city;
@Column(name = "DISTRICT")
private String district;
@Column(name = "STATE")
private String state;
@Column(name = "PINCODE")
private String pincode;
@Column(name = "MANAGERID")
private String managerid;
@Column(name = "MANAGERNAME")
private String managerName;
异常追踪: -
017-08-30 20:22:22.919 DEBUG 9560 --- [nio-9000-exec-7] org.hibernate.SQL : update NEWEMPLOYEE set CITY=?, DISTRICT=?, FIRSTNAME=?, LASTNAME=?, MANAGERNAME=?, MANAGERID=?, PINCODE=?, STATE=?, STREET=?, VILLAGE=? where ID=?
2017-08-30 20:22:22.930 INFO 9560 --- [nio-9000-exec-7] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
2017-08-30 20:22:22.935 ERROR 9560 --- [nio-9000-exec-7] o.s.batch.core.step.AbstractStep : Encountered an error executing step employeeSlaveStep in job EmployeeJob
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:67)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:54)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3134)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3013)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3393)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:145)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:582)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:456)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1282)
at org.springframework.batch.item.database.HibernateItemWriter.write(HibernateItemWriter.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)