Я получаю нижеупомянутую трассировку стека при вставке данных через пакетную вставку с использованием Hibernate (изначально я установил размер пакета 6)
Hibernate:
select
next value for id_seq_gen
Hibernate:
insert
into
tbl_fresh_base
(ASM_NUMBER, BRAND , CIRCLE_TEAM, CIRCLE_TEAM1, CIRCLE_TEAM2, CIRCLE_TEAM3, DIST_Number, DSE_MOB_NO, RETAILER_NUMBER, TSM_NUMBER, UB_COUNT, ZM_NUMBER, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
tbl_fresh_base
(ASM_NUMBER, BRAND , CIRCLE_TEAM, CIRCLE_TEAM1, CIRCLE_TEAM2, CIRCLE_TEAM3, DIST_Number, DSE_MOB_NO, RETAILER_NUMBER, TSM_NUMBER, UB_COUNT, ZM_NUMBER, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
tbl_fresh_base
(ASM_NUMBER, BRAND , CIRCLE_TEAM, CIRCLE_TEAM1, CIRCLE_TEAM2, CIRCLE_TEAM3, DIST_Number, DSE_MOB_NO, RETAILER_NUMBER, TSM_NUMBER, UB_COUNT, ZM_NUMBER, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
tbl_fresh_base
(ASM_NUMBER, BRAND , CIRCLE_TEAM, CIRCLE_TEAM1, CIRCLE_TEAM2, CIRCLE_TEAM3, DIST_Number, DSE_MOB_NO, RETAILER_NUMBER, TSM_NUMBER, UB_COUNT, ZM_NUMBER, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
tbl_fresh_base
(ASM_NUMBER, BRAND , CIRCLE_TEAM, CIRCLE_TEAM1, CIRCLE_TEAM2, CIRCLE_TEAM3, DIST_Number, DSE_MOB_NO, RETAILER_NUMBER, TSM_NUMBER, UB_COUNT, ZM_NUMBER, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
tbl_fresh_base
(ASM_NUMBER, BRAND , CIRCLE_TEAM, CIRCLE_TEAM1, CIRCLE_TEAM2, CIRCLE_TEAM3, DIST_Number, DSE_MOB_NO, RETAILER_NUMBER, TSM_NUMBER, UB_COUNT, ZM_NUMBER, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
org.hibernate.exception.SQLGrammarException: could not execute batch
vodabaseupload - 2020-01-09 14:56:06,037 [http-bio-8080-exec-3] ERROR o.h.e.jdbc.spi.SqlExceptionHelper -
Cannot insert explicit value for identity column in table 'tbl_fresh_base' when IDENTITY_INSERT is set to OFF.
vodabaseupload - 2020-01-09 14:56:06,039 [http-bio-8080-exec-3] ERROR o.h.e.j.batch.internal.BatchingBatch -
HHH000315: Exception executing batch [could not execute batch]
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:136)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.addToBatch(BatchingBatch.java:98)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1258)
at com.spice.vodabaseupload.dao.impl.BaseUploadDaoImpl.upload(BaseUploadDaoImpl.java:245)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy46.upload(Unknown Source)
at com.spice.vodabaseupload.controller.LoginController.upload(LoginController.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:870)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1136)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.BatchUpdateException: Cannot insert explicit value for identity column in table 'tbl_fresh_base' when IDENTITY_INSERT is set to OFF.
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeBatch(SQLServerPreparedStatement.java:1824)
at org.hibernate.engine.jdbc.batch.internal.BatchingBatch.performExecution(BatchingBatch.java:127)
... 65 more
vodabaseupload - 2020-01-09 14:56:06,080 [http-bio-8080-exec-3] INFO c.s.v.controller.LoginController -
final resp is [null]
Вот мой класс сущностей BaseUploadEntity. java
package com.spice.vodabaseupload.beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="tbl_fresh_base")
public class BaseUploadEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "id_gen")
@SequenceGenerator(name="id_gen",sequenceName = "id_seq_gen")
@Column(name="id")
private long id;
@Column(name="RETAILER_NUMBER")
private String RETAILERNUMBER;
@Column(name="DSE_MOB_NO")
private String DSE_MOB_NO;
@Column(name="DIST_Number")
private String DIST_Number;
@Column(name="TSM_NUMBER")
private String TSM_NUMBER;
@Column(name="ASM_NUMBER")
private String ASM_NUMBER;
@Column(name="ZM_NUMBER")
private String ZM_NUMBER;
@Column(name="CIRCLE_TEAM")
private String CIRCLE_TEAM;
@Column(name="CIRCLE_TEAM1")
private String CIRCLE_TEAM1;
@Column(name="CIRCLE_TEAM2")
private String CIRCLE_TEAM2;
@Column(name="CIRCLE_TEAM3")
private String CIRCLE_TEAM3;
@Column(name="BRAND ")
private String Brand;
@Column(name="UB_COUNT")
private String UB_Count;
//Here are getters and Setters//
}
Вот файл Application.properties file
jdbc.driverClassName = com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url = jdbc:sqlserver://localhost:1433;database=demo
jdbc.username = bgm
jdbc.password = bgm
hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect
hibernate.show_sql = true
hibernate.format_sql = true
hibernate.hbm2ddl.auto = update
hibernate.jdbc.batch_size = 6
hibernate.order_inserts = true
Создание SessionFactory с JavaConfig следующим образом
package com.spice.vodabaseupload.configuration;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan({ "com.spice.vodabaseupload.configuration" })
@PropertySource(value = { "classpath:application.properties" })
public class ProjectConfiguration extends WebMvcConfigurerAdapter {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.spice.vodabaseupload.beans" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
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.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
properties.put("hibernate.jdbc.batch_size", environment.getRequiredProperty("hibernate.jdbc.batch_size"));
properties.put("hibernate.order_inserts", true);
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
}
Вот мой BaseUploadDaoImpl.class
FileInputStream inputStream = new FileInputStream(baseFileToCreate);
Workbook workbook = getRelevantWorkbook(inputStream, baseFileToCreate.toString());
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
int count = 0;
while (iterator.hasNext()) {
BaseUploadEntity entity = new BaseUploadEntity();
count++;
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getColumnIndex() == 0) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setRETAILERNUMBER(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setRETAILERNUMBER(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
else if (cell.getColumnIndex() == 1) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setDSE_MOB_NO(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setDSE_MOB_NO(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
else if (cell.getColumnIndex() == 2) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setDIST_Number(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setDIST_Number(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
else if (cell.getColumnIndex() == 3) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setTSM_NUMBER(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setTSM_NUMBER(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
else if (cell.getColumnIndex() == 4) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setASM_NUMBER(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setASM_NUMBER(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
else if (cell.getColumnIndex() == 5) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setZM_NUMBER(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setZM_NUMBER(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
else if (cell.getColumnIndex() == 6) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setCIRCLE_TEAM(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setCIRCLE_TEAM(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
else if (cell.getColumnIndex() == 7) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setCIRCLE_TEAM1(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setCIRCLE_TEAM1(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
else if (cell.getColumnIndex() == 8) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setCIRCLE_TEAM2(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setCIRCLE_TEAM2(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
else if (cell.getColumnIndex() == 9) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setCIRCLE_TEAM3(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setCIRCLE_TEAM3(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
else if (cell.getColumnIndex() == 10) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setBrand(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setBrand(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
else if (cell.getColumnIndex() == 11) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
entity.setUB_Count(cell.getStringCellValue());
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
entity.setUB_Count(NumberToTextConverter.toText(cell.getNumericCellValue()));
}
}
}
session.save(entity);
if (count % batchSize == 0) {
LOGGER.info("Inserting " + count + " Record");
session.flush();
session.clear();
}
}
inputStream.close();
Однако без пакетной вставки код работает нормально