hashCode()导致java.lang.StackOverflowError

时间:2016-01-12 16:45:16

标签: java spring stack-overflow

我有一张表格,其中

<form:form method="post" commandName="post" htmlEscape="false">
    <form:input cssClass="input-field" path="postTitle" placeholder="Post title"/>            
    <form:textarea id="editor" placeholder="Post" path="postBody"/>
    <input type="hidden" id="tagQuery" type="text" placeholder="tags" class="input-field"/><br> 
    <ul id="mytags"></ul><br><br>          
    <form:select style="visibility : hidden;" id="tags" path="tags">
    </form:select>
    <input type="submit" value="Save Post"/>    
</form:form>

我使用tagit lib获取标签,因为我已配置它

select = document.querySelector("select[name='tags']");    
jQuery(document).ready(function() {
    jQuery("#mytags").tagit({
        singleField: true,
        singleFieldNode: $('#tagQuery'),
        allowSpaces: true,
        minLength: 2,
        removeConfirmation: true,
        tagSource: function( request, response ) {
            //console.log("1");
             $.getJSON("/app/getTags", request, function(result) {
                response($.map(result, function(item) {
                    return {
                        // following property gets displayed in drop down
                        label: item.tagTitle,
                        // following property gets entered in the textbox
                        value: item.tagTitle,
                        // following property is added for our own use
                        tag_url: "http://" + window.location.host + "/" + item.tagId + "/" + item.tagTitle
                    }
                }));
            });              
        },
        afterTagAdded : function(event, ui) {
             var option = document.createElement("option");
      console.log(ui);
      console.log(idValue);
                option.value = ui.tagLabel;
                option.text =  ui.tagLabel;
                option.selected = 'selected';
                select.appendChild(option);
        },
        afterTagRemoved : function(event, ui) {
            select.removeChild(select.querySelector('[value="'+ui.tagLabel+'"]'));
        }
        });
    });

最后我有控制器的东西

public class PostController {

    @Autowired
    private BlogService serviceImplementation;

    @ModelAttribute("post")
    public Post constructPost() {
        return new Post();
    }
    @ModelAttribute("tag")
    public Tag constructTag() {
        return new Tag();
    }

    @InitBinder
    public void initBinderOne(WebDataBinder binder) {
        binder.registerCustomEditor(Tag.class,
                new PropertyEditorSupport() {
                    @Override
                    public void setAsText(String text) {
                        Tag tag = serviceImplementation.getTagById(Long
                                .parseLong(text));
                        setValue(tag);
                    }
                });     
    }           

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String createPost(Model model) {     
        return "new-post";
    }

    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public String savePost(Model model, @ModelAttribute("post") Post post) {

        serviceImplementation.savePost(post);
        return "redirect:/";
    }
}

当我保存帖子时,我收到错误

the request sent by the client was syntactically incorrect

我发现,当我保存帖子发送标签为

时就会发生这种情况
option.value = ui.tagLabel;
option.text =  ui.tagLabel;
option.selected = 'selected';
select.appendChild(option);

将假设值设为ui.tagLabel,为

<option value="Android">Android</option>

我正在改变

select = document.querySelector("select[name='tags']");
var idValue; //HERE I ADDED var to get 
jQuery(document).ready(function() {
    jQuery("#mytags").tagit({
        singleField: true,
        singleFieldNode: $('#tagQuery'),
        allowSpaces: true,
        minLength: 2,
        removeConfirmation: true,
        tagSource: function( request, response ) {
            //console.log("1");
             $.getJSON("/app/getTags", request, function(result) {
                response($.map(result, function(item) {
                    idValue = item.tagId; // HERE I ADDED var to get id to set as value in option *
                    return {
                        // following property gets displayed in drop down
                        label: item.tagTitle,
                        // following property gets entered in the textbox
                        value: item.tagTitle,
                        // following property is added for our own use
                        tag_url: "http://" + window.location.host + "/" + item.tagId + "/" + item.tagTitle
                    }
                }));
            });              
        },
        afterTagAdded : function(event, ui) {
             var option = document.createElement("option");
      console.log(ui);
      console.log(idValue);
                option.value = idValue;//HERE When I add idValue to option I always get the id of last tag, let say there are 6 tags in ajax response, idValue is always 6. 
                option.text =  ui.tagLabel;
                option.selected = 'selected';
                select.appendChild(option);
        },
        afterTagRemoved : function(event, ui) {
            select.removeChild(select.querySelector('[value="'+ui.tagLabel+'"]'));
        }
        });
    });

然后将ajax respose设置设置为前一个脚本

 option.value = ui.tagLabel;
 option.text =  ui.tagLabel;
 option.selected = 'selected';
 select.appendChild(option);

我更改了initBinder()

@InitBinder
    public void initBinderOne(WebDataBinder binder) {
        binder.registerCustomEditor(Tag.class,
                new PropertyEditorSupport() {
                    @Override
                    public void setAsText(String text) {                            
                        Tag tag = serviceImplementation.getTagByName(text);
                        setValue(tag);
                    }
                });     
    }       

public Tag getTagByName(String tagTitle) {
        // TODO Auto-generated method stub
        return em.createQuery("from Tag t where t.tagTitle = :tagTitle", Tag.class)
                .setParameter("tagTitle", tagTitle).getSingleResult();
    }

此时我获得了无限的堆栈跟踪,有些线条......

HTTP Status 500 - Handler processing failed; nested exception is java.lang.StackOverflowError

type Exception report

message Handler processing failed; nested exception is java.lang.StackOverflowError

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

exception

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.StackOverflowError
    org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1303)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:977)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.StackOverflowError
    java.nio.CharBuffer.<init>(Unknown Source)
    java.nio.HeapCharBuffer.<init>(Unknown Source)
    java.nio.CharBuffer.wrap(Unknown Source)
    sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
    sun.nio.cs.StreamEncoder.write(Unknown Source)
    java.io.OutputStreamWriter.write(Unknown Source)
    java.io.BufferedWriter.flushBuffer(Unknown Source)
    java.io.PrintStream.write(Unknown Source)
    java.io.PrintStream.print(Unknown Source)
    java.io.PrintStream.println(Unknown Source)
    org.apache.tomcat.util.log.SystemLogHandler.println(SystemLogHandler.java:264)
    org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:111)
    org.hibernate.engine.jdbc.spi.SqlStatementLogger.logStatement(SqlStatementLogger.java:94)
    org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:181)
    org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:160)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.prepareQueryStatement(AbstractLoadPlanBasedLoader.java:257)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:201)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
    org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:100)
    org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693)
    org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:92)
    org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1897)
    org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:558)
    org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:260)
    org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
    org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
    org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:447)
    com.rhcloud.app.entity.Tag.hashCode(Tag.java:77)
    java.util.HashMap.put(Unknown Source)
    java.util.HashSet.add(Unknown Source)
    java.util.AbstractCollection.addAll(Unknown Source)
    org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:344)
    org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:251)
    org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:238)
    org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:211)
    org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:168)
    org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:255)
    org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:218)
    org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:140)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
    org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:100)
    org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693)
    org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:92)
    org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1897)
    org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:558)
    org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:260)
    org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
    org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
    org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:447)
    com.rhcloud.app.entity.Post.hashCode(Post.java:92)
    java.util.HashMap.put(Unknown Source)
    java.util.HashSet.add(Unknown Source)
    java.util.AbstractCollection.addAll(Unknown Source)
    org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:344)
    org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:251)
    org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:238)
    org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:211)
    org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:168)
    org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:255)
    org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:218)
    org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:140)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
    org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
    org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:100)
    org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:693)
    org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:92)
    org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1897)
    org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:558)
    org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:260)
    org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
    org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
  

我想弄清楚为什么这个 hashCode()会在我的Spring Web App中导致java.lang.StackOverflowError   发布hashCode()是

@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((postBody == null) ? 0 : postBody.hashCode());
        result = prime * result + ((postId == null) ? 0 : postId.hashCode());
        result = prime * result
                + ((postTitle == null) ? 0 : postTitle.hashCode());
        result = prime * result + ((tags == null) ? 0 : tags.hashCode());
        return result;
    }

标签hashCode()是

@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((posts == null) ? 0 : posts.hashCode());
        result = prime * result
                + ((tagDescription == null) ? 0 : tagDescription.hashCode());
        result = prime * result + ((tagId == null) ? 0 : tagId.hashCode());
        result = prime * result
                + ((tagTitle == null) ? 0 : tagTitle.hashCode());
        return result;
    }

2 个答案:

答案 0 :(得分:7)

posts.hashCode()调用tags.hashCode(),调用posts.hashCode()等等....

你似乎有无限循环。

答案 1 :(得分:1)

如果有人使用来自 Lombok 的 @Data@EqualsAndHashCode 那么他们可以非常简单地排除导致此无限循环的属性.

在该属性上使用波纹管注释:

@EqualsAndHashCode.Exclude

有很多选项可以做到这一点please have a look