Hibernate findById()问题

时间:2015-10-20 06:26:36

标签: hibernate

我在任何Crud操作期间通过hibernate中的findbyId()方法遇到问题(并非总是如此);它正意外地出现(在任何不确定的时间,10%的机会提出问题)。     但在重新加载页面(再次从浏览器发送请求)后,它会获得所需的数据;没问题。

I have checked with all methods of hibernate(4.1.7) for findById such as get(), load(), byId.load(), criteria, but invain.
given my testing code snippet:

public SurveyDefination findById(final long sdId) throws HibernateException {
        /*
         * Session session1 = HibernateUtil.getSessionFactory().openSession();
         * session1.beginTransaction();
         */
        Criteria criteria = session.createCriteria(SurveyDefination.class).add(
                Restrictions.idEq(sdId));
        SurveyDefination surveyDefination = (SurveyDefination) criteria
                .uniqueResult();

        if (surveyDefination == null) {
            int count = 0;
            System.out
                    .println("Before------surveyDefination--------Output --------- :: "
                            + count++ + ":  " + surveyDefination);
        //  session.evict(surveyDefination);
            if (surveyDefination == null && count == 1) {
                surveyDefination = (SurveyDefination) session.get(
                        SurveyDefination.class, sdId);
                System.out
                        .println("Before------surveyDefination--------Output -----get()---- :: "
                                + count++ + ":  " + surveyDefination);
            }
            if (surveyDefination == null)
                try {
                    surveyDefination = (SurveyDefination) session.load(
                            SurveyDefination.class, sdId);
                    System.out
                            .println("Before------surveyDefination--------Output -----load()---- :: "
                                    + count++ + ":  " + surveyDefination);
                } catch (HibernateException e) {
                    logger.error(e.getMessage() + "  ObjectNotFoundException");
                    e.printStackTrace();
                }

            if (surveyDefination == null) {
                surveyDefination = (SurveyDefination) session.byId(
                        SurveyDefination.class).load(sdId);
                logger.info("Before------surveyDefination--------Output -----byId().load()---- :: "
                        + count++ + ":  " + surveyDefination);
            }

            if (surveyDefination == null) {

                criteria = session.createCriteria(SurveyDefination.class).add(
                        Restrictions.idEq(sdId));
                surveyDefination = (SurveyDefination) criteria.uniqueResult();
                System.out
                        .println("Before------surveyDefination--------Output -----createCriteria()---- :: "
                                + count++ + ":  " + surveyDefination);
            }
        }
        System.out
                .println("After -----surveyDefination---------Output --------- :: "
                        + surveyDefination);

        return surveyDefination;
    }


But after all checking if it gets null once, it will never get the result;
but same request by resending immediately, desired output comes out.

Following is the error from my console, plz give a reason for such issue. 

11:06:50,987 INFO  [stdout] (http-localhost-127.0.0.1-8080-4) connection is idle true connection release count 2

11:06:51,012 INFO  [stdout] (http-localhost-127.0.0.1-8080-4) Before------surveyDefination--------Output --------- :: 0:  null

11:06:51,018 INFO  [stdout] (http-localhost-127.0.0.1-8080-4) Before------surveyDefination--------Output -----get()---- :: 1:  null

11:06:51,027 ERROR [stderr] (http-localhost-127.0.0.1-8080-4) org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.ttn.survey.model.SurveyDefination#322]

11:06:51,027 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.internal.SessionFactoryImpl$1$1.handleEntityNotFound(SessionFactoryImpl.java:242)

11:06:51,028 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:248)

11:06:51,028 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:176)

11:06:51,029 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:272)

11:06:51,029 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)

11:06:51,030 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at com.ttn.survey.model.SurveyDefination_$$_javassist_95.toString(SurveyDefination_$$_javassist_95.java)

11:06:51,030 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at java.lang.String.valueOf(String.java:2847)

11:06:51,030 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at java.lang.StringBuilder.append(StringBuilder.java:128)

11:06:51,031 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at com.ttn.survey.controller.implementations.dao.SurveyDefinationDAOImpl.findById(SurveyDefinationDAOImpl.java:132)

11:06:51,031 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at com.ttn.survey.controller.implementations.client.ClientSurveyActionImpl.findSurveyInfoById(ClientSurveyActionImpl.java:213)

11:06:51,032 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at com.ttn.survey.view.client.SurveyControllerServlet.doGet(SurveyControllerServlet.java:199)

11:06:51,032 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)

11:06:51,032 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

11:06:51,033 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

11:06:51,033 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

11:06:51,034 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:840)

11:06:51,034 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:622)

11:06:51,034 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:560)

11:06:51,035 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:488)

11:06:51,035 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)

11:06:51,036 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)

11:06:51,036 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)

11:06:51,037 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)

11:06:51,037 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)

11:06:51,037 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

11:06:51,038 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

11:06:51,038 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

11:06:51,039 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

11:06:51,039 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)

11:06:51,039 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

11:06:51,040 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

11:06:51,041 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

11:06:51,041 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

11:06:51,042 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

11:06:51,042 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

11:06:51,043 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

11:06:51,043 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at java.lang.Thread.run(Thread.java:745)

11:06:51,043 ERROR [stderr] (http-localhost-127.0.0.1-8080-4) org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.ttn.survey.model.SurveyDefination#322]

11:06:51,044 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.internal.SessionFactoryImpl$1$1.handleEntityNotFound(SessionFactoryImpl.java:242)

11:06:51,044 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:248)

11:06:51,045 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:180)

11:06:51,045 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:272)

11:06:51,046 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)

11:06:51,046 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at com.ttn.survey.model.SurveyDefination_$$_javassist_95.toString(SurveyDefination_$$_javassist_95.java)

11:06:51,047 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at java.lang.String.valueOf(String.java:2847)

11:06:51,047 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at java.lang.StringBuilder.append(StringBuilder.java:128)

11:06:51,047 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at com.ttn.survey.controller.implementations.dao.SurveyDefinationDAOImpl.findById(SurveyDefinationDAOImpl.java:157)

11:06:51,048 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at com.ttn.survey.controller.implementations.client.ClientSurveyActionImpl.findSurveyInfoById(ClientSurveyActionImpl.java:213)

11:06:51,048 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at com.ttn.survey.view.client.SurveyControllerServlet.doGet(SurveyControllerServlet.java:199)

11:06:51,049 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)

11:06:51,049 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

11:06:51,049 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

11:06:51,050 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

11:06:51,050 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:840)

11:06:51,051 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:622)

11:06:51,051 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:560)

11:06:51,051 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:488)

11:06:51,052 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)

11:06:51,052 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)

11:06:51,053 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)

11:06:51,053 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)

11:06:51,054 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)

11:06:51,054 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

11:06:51,054 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

11:06:51,055 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

11:06:51,055 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

11:06:51,056 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)

11:06:51,057 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

11:06:51,058 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

11:06:51,058 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

11:06:51,059 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

11:06:51,059 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

11:06:51,059 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

11:06:51,060 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

11:06:51,060 ERROR [stderr] (http-localhost-127.0.0.1-8080-4)   at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

这个问题

 surveyDefination = (SurveyDefination) session.load(
                            SurveyDefination.class, sdId);

您无法使用load()方法加载不存在的持久性。所以你在SurveyDefination的toString()中得到一个例外(因为我认为,因为你不提供行号)

System.out.println("Before------surveyDefination--------Output 
-----load()---- :: " + count++ + ":  " + surveyDefination);

P.S。如果你想看一些使用Hibernate的技巧,你可以看到它here