为有自增字段的实体类插入数据,报错及措施

报错如下:

[11-19 11:07:58] [http-bio-8080-exec-9] [ERROR][org.hibernate.engine.jdbc.spi.SqlExceptionHelper-146]-当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'TMe_MerchandiseCInfo' 中的标识列插入显式值。
[11-19 11:07:59] [http-bio-8080-exec-9] [ERROR][org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl-175]-HHH000352: Unable to release batch statement...
[11-19 11:07:59] [http-bio-8080-exec-9] [ERROR][org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl-176]-HHH000246: SQLException escaped proxy
java.sql.SQLException: org.apache.commons.dbcp.DelegatingPreparedStatement with address: "SQLServerPreparedStatement:108" is closed.
at org.apache.commons.dbcp.DelegatingStatement.checkOpen(DelegatingStatement.java:137)
at org.apache.commons.dbcp.DelegatingStatement.clearBatch(DelegatingStatement.java:294)
at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.releaseStatements(AbstractBatchImpl.java:171)
at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.release(AbstractBatchImpl.java:205)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.close(JdbcCoordinatorImpl.java:183)
at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.close(TransactionCoordinatorImpl.java:292)
at org.hibernate.internal.SessionImpl.close(SessionImpl.java:354)
at org.springframework.orm.hibernate4.SessionFactoryUtils.closeSession(SessionFactoryUtils.java:105)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:160)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
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:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:106)
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:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)


解决办法:为有自增属性的字段的构造器加insertable,updateable,之后就可以不给id赋值,正常插入更新

  @Column(name = "ID" ,insertable = false,updatable = false)
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

若自增字段为主键,则在get、set的@Id下加上  @GeneratedValue(strategy =GenerationType.IDENTITY)

你可能感兴趣的:(IDEA,数据库)