Hibernate中提交后的数据库自动更新

Hibernate中提交后的数据库自动更新,hibernate,struts2,Hibernate,Struts2,我有一个网站,使用Struts2和Hibernate,MySQL,使用Tomcat6服务器,从一家公司的员工那里获取调查结果。这是获取事件问题的方法,事件问题使用UTF-8编码。这是我的代码: public String getEventQuestion() { try { event = surveyEventController.getEvent(this.getId()); surveyQuestion

我有一个网站,使用Struts2和Hibernate,MySQL,使用Tomcat6服务器,从一家公司的员工那里获取调查结果。这是获取事件问题的方法,事件问题使用UTF-8编码。这是我的代码:

public String getEventQuestion() {
          try {
                 event = surveyEventController.getEvent(this.getId());
                 surveyQuestions = surveyQuestionController.getQuestion(this.getId());
                 if (surveyQuestions != null) {
                       return Constants.SUCCESS;
                 } else {
                       return Constants.FAIL;
                 }
          } catch (Exception e) {
                 logger.error("[Exception]getSurvey: " + e.getMessage(), e);
                 return Constants.FAIL;
          }
   }
从控制器获取问题方法:

@SuppressWarnings("unchecked")
   public List<SurveyQuestion> getQuestion(Long eventID)
   {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          List<SurveyQuestion> listQuestion = null;
          try
          {
                 session.getTransaction().begin(); 

                 listQuestion = (List<SurveyQuestion>)session.createQuery("from SurveyQuestion where event_id="+eventID.toString()).list();
          session.getTransaction().commit();
          }
          catch(Exception e)
          {
                 logger.error("[Exception] getQuestion SurveyQuestion: ",e);
          session.getTransaction().rollback();
          }
          if(listQuestion!=null && !listQuestion.isEmpty())
          {
                 return listQuestion;
          }
          else
          {
                 return null;
          }
   }
@SuppressWarnings(“未选中”)
公共列表getQuestion(长事件ID)
{
会话会话=HibernateUtil.getSessionFactory().getCurrentSession();
List listQuestion=null;
尝试
{
session.getTransaction().begin();
listQuestion=(List)session.createQuery(“来自SurveyQuestion where event_id=“+eventID.toString()).List();
session.getTransaction().commit();
}
捕获(例外e)
{
logger.error(“[Exception]getQuestion SurveyQuestion:”,e);
session.getTransaction().rollback();
}
if(listQuestion!=null&&!listQuestion.isEmpty())
{
返回列表问题;
}
其他的
{
返回null;
}
}
所有jsp页面编码也是UTF-8

在我第一次访问网站时,正如我预期的那样,内容成功加载了UTF-8内容


问题是在我按F5刷新页面后,内容会自动更改为非UTF-8编码。我调试并认识到,当运行line session.getTransaction().commit()时,数据库会自动更新为非UTF-8编码内容中的listQuestion,可能是ISO或类似内容,所有特殊字符都会改为?。如何解决这个问题?

我假设我可以通过

<property name="connection.url">
        jdbc:mysql://localhost:3306/survey?useUnicode=true&amp;amp;characterEncoding=UTF-8;
    </property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>

jdbc:mysql://localhost:3306/survey?useUnicode=true&amp;characterEncoding=UTF-8;
但是当上述方法不起作用时,我找到了另一种解决方案,在hibernate.cfg.xml中,您必须通过

<property name="connection.url">
        jdbc:mysql://localhost:3306/survey?useUnicode=true&amp;amp;characterEncoding=UTF-8;
    </property>
<property name="hibernate.connection.characterEncoding">UTF-8</property>
UTF-8

它工作正常。感谢您对Roman C的支持。

什么是数据库?@Roman:MySQL,我已经将db编码设置为UTF-8。第一次加载时工作正常,按F5时出现问题:(您的JSP的页面编码是什么?也是UTF-8。开始时,页面编码是ISO,并且内容不像我预期的那样加载,然后我将所有内容配置为UTF-8编码,这样页面就完全加载了,但只有在第一次加载时才加载。Tomcat 6.0 Roman,很抱歉缺少信息:(