0

我正在尝试向我的论坛添加主题,每次创建论坛对象时都会收到此错误:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.hibernate.AssertionFailure: null id in com.versusfootball.hibernate.entity.ForumTopic entry (don't flush the Session after an exception occurs)
    org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
    org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
    org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
    org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
    org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
    org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
    org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    com.versusfootball.dao.DAO.rollback(DAO.java:46)
    com.versusfootball.dao.ForumTopicDAO.create(ForumTopicDAO.java:31)
    com.versusfootball.struts.main.forum.SaveForumSubject.execute(SaveForumSubject.java:46)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:616)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
    com.versusfootball.filters.InitSession.doFilter(InitSession.java:81)
    com.versusfootball.filters.DisableUrlSessionFilter.doFilter(DisableUrlSessionFilter.java:87)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.

Apache Tomcat/6.0.29

这是我的论坛构造函数:

public Forum create (Forum forum) throws AppException {
    try {
        begin();
        if(forum.getId() != null) {
            getSession().merge(forum);
        }
        else {
            getSession().save(forum);
        }
        commit();
        close();
        return forum;
    }
    catch (HibernateException e) {
        log.error("Error creating forum : " + e.getCause().getMessage());
        rollback();
        throw new AppException(e.getCause().getMessage());
    }
}

这是我的提交执行:

package com.versusfootball.struts.main.forum;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;
import com.versusfootball.dao.ForumDAO;
import com.versusfootball.dao.ForumMessageDAO;
import com.versusfootball.dao.ForumTopicDAO;
import com.versusfootball.exceptions.AppException;
import com.versusfootball.hibernate.entity.Forum;
import com.versusfootball.hibernate.entity.ForumMessage;
import com.versusfootball.hibernate.entity.ForumTopic;
import com.versusfootball.hibernate.entity.Personne;

public class SaveForumSubject extends ActionSupport implements ServletRequestAware {
    /**
     * 
     */
    private static final long serialVersionUID = -2131201082127318301L;
    private static final Logger log = Logger.getLogger(SaveForumSubject.class);  

    public String execute() {
        try {
            HttpSession session = request.getSession();
            Personne usager = (Personne)session.getAttribute("usagerFoot");
            if(usager == null) {
                return ERROR;
            }

            Date today = new Date();

            forum = forumDAO.getById(forumId);
            ForumTopic newTopic = new ForumTopic();
            newTopic.setNbMessages(0);
            newTopic.setNbViews(0);
            newTopic.setForum(forum);
            newTopic.setCreationDate(today);
            newTopic.setTitle(msgTitle);
            newTopic.setAuthor(usager);
            newTopic = ftDAO.create(newTopic);
            subjectId = newTopic.getId();
            ForumMessage newMsg = new ForumMessage();

            newMsg.setAuthor(usager);            
            newMsg.setMessageDate(today);
            message = message.replaceAll("\n", "<br />");
            newMsg.setText(message);            
            newMsg.setTopic(newTopic);
            newMsg.setNiveau(0);
            newMsg = fmDAO.create(newMsg);

            messageId = newMsg.getId();

            forum.setNbMessages(forum.getNbMessages() + 1);
            forum.setNbTopics(forum.getNbTopics() + 1);
            forum.setLastMessage(newMsg);
            newTopic.setLastMessage(newMsg);
            newTopic = ftDAO.create(newTopic);

            forumDAO.create(forum);

        } catch (AppException e) {
            // TODO Auto-generated catch block
            log.error(e.getMessage());
        }
        return SUCCESS;     
    }

    private ForumDAO forumDAO = new ForumDAO();
    private ForumMessageDAO fmDAO = new ForumMessageDAO();
    private ForumTopicDAO ftDAO = new ForumTopicDAO();

    private HttpServletRequest request;

    private String msgTitle = "";
    private String message = "";
    private Integer forumId;
    private Integer messageId;
    private Integer subjectId;

    private Forum forum;

    public void setMsgTitle(String msgTitle) {
        this.msgTitle = msgTitle;
    }
    public String getMsgTitle() {
        return msgTitle;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }

    public void setForumId(Integer forumId) {
        this.forumId = forumId;
    }
    public Integer getForumId() {
        return forumId;
    }

    public Integer getMessageId() {
        return messageId;
    }

    public Forum getForum() {
        return forum;
    }

    public Integer getSubjectId() {
        return subjectId;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;        
    }
}

有人知道我该如何解决这个问题吗?

4

2 回答 2

0

您需要检查您的 id(与主键对应的字段)。它必须为空。你是如何生成id的?您是否为它定义了适当的策略?

于 2012-10-30T06:24:37.080 回答
0

从你的 catch 块调用 rollback() 时发生异常。

getSession().save(forum);通话中或commit();通话中可能存在异常。

(消息说 id 为空)。

无论如何,您只是通过记录getMessage()而不是整个异常来隐藏真正的异常。

尝试打印原始异常,以便弄清楚发生了什么。

于 2012-10-29T15:02:05.130 回答