0

我是 hibernate 新手,我正在开发一个使用 MySQl、Hibernate 和 Struts 的应用程序。对于给定的代码,我总是收到以下错误。我为这个错误做了谷歌,但那些线程没有帮助。

错误

javax.servlet.ServletException: Could not execute JDBC batch update
    at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:516)
    ....

代码

Session session = new HibernateSession().currentSession(this.getServlet().getServletContext());
        if(auctionform.getId() == null || auctionform.getId().intValue() == 0){
            auction.setId(null);
            auctionform.setId(null);
        }else{
            auction.setId(auctionform.getId());
            Criteria criteria = session.createCriteria(Auction.class);
            criteria.add(Restrictions.idEq(Integer.valueOf(req.getParameter("id"))));
            auction = (Auction) criteria.uniqueResult();

        }

        Boolean isAuctionActive = false;
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        Date auctionEndingDate = DateUtils.getDate(req, "auctionenddate");
        Date todaysDate = formatter.parse(formatter.format(currentDate.getTime()));

        if(auctionEndingDate.after(todaysDate)){
            isAuctionActive = true;
        }

        auction.setDescription(auctionform.getDescription());
        auction.setDate(auctionform.getDate());
        auction.setCurrentprice(auctionform.getCurrentprice());
        ....some code

        Attachment attachment1 = AttachmentUtils.saveAttachment(auctionform, session, auction, 0);
        Attachment attachment2 = AttachmentUtils.saveAttachment(auctionform, session, auction, 1);
        Attachment attachment3 = AttachmentUtils.saveAttachment(auctionform, session, auction, 2);


        Transaction transaction = session.beginTransaction();
        if(attachment1!=null){
            session.save(attachment1.getDbFile());
            session.save(attachment1.getThmnailDbFile());
            session.save(attachment1);
            auction.setFirstimage(attachment1);
        }
        if(attachment2!=null){
            session.save(attachment2.getDbFile());
            session.save(attachment2.getThmnailDbFile());
            session.save(attachment2);
            auction.setSecoundimage(attachment2);
        }
        if(attachment3 != null){
            session.save(attachment3.getDbFile());
            session.save(attachment3.getThmnailDbFile());
            session.save(attachment3);
            auction.setSignimage(attachment3);
        }

        session.saveOrUpdate(auction);
        transaction.commit();
        session.flush();

我总是transaction.commit()在线上遇到这个错误。我不明白出了什么问题。

更新:

我设置了 log4j,完整的堆栈跟踪如下。

完整的堆栈跟踪

Caused by: java.sql.BatchUpdateException: Incorrect string value: '\xE5\x8F\x96\xE3\x82\x8A...' for column 'BIDDERRESTRICTION' at row 1
    at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:647)
    at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
    at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
    ... 34 more
2012-10-25 17:26:29 [action] [ERROR] Servlet.service() for servlet action threw exception
java.sql.BatchUpdateException: Incorrect string value: '\xE5\x8F\x96\xE3\x82\x8A...' for column 'BIDDERRESTRICTION' at row 1
    at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:647)
    at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
    at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
    at net.startbahn.web.app.PostAuctionVPAction.execute(PostAuctionVPAction.java:175)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at net.startbahn.filters.PrivilegeManageFilter.doFilter(PrivilegeManageFilter.java:112)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at net.startbahn.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:121)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Thread.java:722)

我试图在列中存储一些日语内容,所以我尝试将表的排序规则更新为COLLATE = ujis_japanese_ciUTF-8,但仍然没有效果。

解决方案:

将表的排序规则及其所有字段设置为utf8-utf8_bin对我来说是诀窍。

4

1 回答 1

0

在 jdbc 连接中设置 UTF8 字符编码。这可以在 hibernate.cfg.xml 文件中完成,例如

<property name="connection.url">
    jdbc:mysql://db-server:3306/databasename?useUnicode=true&amp;characterEncoding=utf8
</property>

(也许这两个选项中只有一个是必要的。我没有尝试,我不使用日语;。)

于 2012-10-25T12:42:20.560 回答