為有自增字段的實體類插入數據,報錯及措施


報錯如下:

[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)


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com