java - Hibernate why is creating n query instead of 1? -


hy want load jobseekers jobseeker table single query , criteria, , hibernate instead of writing 1 query write n query n jobseeker, , dont have idea why please help. jobseeker class:

@entity @table(name="job_seeker")  public class jobseeker extends baseentity{  @column(name = "name", length = 128, nullable = false) @expose private string name;  @onetoone @joincolumn(name = "user_id") private user user;  . . . 

this user class:

 @entity  @table(name="user_auth")  public class user extends baseentity {   @column(name = "username", nullable = false, length = 64, unique = true)  @size(max=64)  @expose  @notempty  private string username;   @column(name = "password", nullable = false, length = 64)  @size(min=6,max=64)  private string password;   @onetoone(mappedby = "user", cascade=cascadetype.all)  private usersettings usersettings;    @onetoone(mappedby = "user", fetch = fetchtype.lazy, cascade=cascadetype.all)  @fetch(fetchmode.select)  private jobseeker jobseeker; . . . 

and usersettings class:

@entity @table(name="user_settings")  public class usersettings extends baseentity{  @onetoone @joincolumn(name = "user_id")    @expose private user user; . . . 

and findall select:

session session = getcurrentsession();  criteria criteria = session.createcriteria(jobseeker.class, "jobseeker")         .setfirstresult(datatablefilter.idisplaystart)         .setmaxresults(datatablefilter.idisplaylength); list<?> list = new arraylist(); list = criteria.list(); 

and makes querys:

hibernate: select this_.id id1_16_3_, this_.created_by created_2_16_3_, this_.created_date created_3_16_3_, this_.modified_by modified4_16_3_, this_.modified_date modified5_16_3_, this_.version version6_16_3_, this_.adress adress7_16_3_, this_.city city8_16_3_, this_.forgot_password forgot_p9_16_3_, this_.time_forgot_password time_fo10_16_3_, this_.name name11_16_3_, this_.not_seen not_see12_16_3_, this_.phone phone13_16_3_, this_.picture_path picture14_16_3_, this_.user_id user_id15_16_3_, user2_.id id1_26_0_, user2_.created_by created_2_26_0_, user2_.created_date created_3_26_0_, user2_.modified_by modified4_26_0_, user2_.modified_date modified5_26_0_, user2_.version version6_26_0_, user2_.email email7_26_0_, user2_.facebook_id facebook8_26_0_, user2_.hash_code hash_cod9_26_0_, user2_.linkedin_id linkedi10_26_0_, user2_.password passwor11_26_0_, user2_.role_id role_id15_26_0_, user2_.status status12_26_0_, user2_.twitter_id twitter13_26_0_, user2_.username usernam14_26_0_, role3_.id id1_22_1_, role3_.created_by created_2_22_1_, role3_.created_date created_3_22_1_, role3_.modified_by modified4_22_1_, role3_.modified_date modified5_22_1_, role3_.version version6_22_1_, role3_.description descript7_22_1_, role3_.name name8_22_1_, usersettin4_.id id1_27_2_, usersettin4_.created_by created_2_27_2_, usersettin4_.created_date created_3_27_2_, usersettin4_.modified_by modified4_27_2_, usersettin4_.modified_date modified5_27_2_, usersettin4_.version version6_27_2_, usersettin4_.last_news_send_date last_new7_27_2_, usersettin4_.preferred_language preferre8_27_2_, usersettin4_.recieve_newsletter recieve_9_27_2_, usersettin4_.user_id user_id10_27_2_ job_seeker this_ left outer join user_auth user2_ on this_.user_id=user2_.id left outer join role role3_ on user2_.role_id=role3_.id left outer join user_settings usersettin4_ on user2_.id=usersettin4_.user_id order this_.name asc hibernate: select jobseeker0_.id id1_16_3_, jobseeker0_.created_by created_2_16_3_, jobseeker0_.created_date created_3_16_3_, jobseeker0_.modified_by modified4_16_3_, jobseeker0_.modified_date modified5_16_3_, jobseeker0_.version version6_16_3_, jobseeker0_.adress adress7_16_3_, jobseeker0_.city city8_16_3_, jobseeker0_.forgot_password forgot_p9_16_3_, jobseeker0_.time_forgot_password time_fo10_16_3_, jobseeker0_.name name11_16_3_, jobseeker0_.not_seen not_see12_16_3_, jobseeker0_.phone phone13_16_3_, jobseeker0_.picture_path picture14_16_3_, jobseeker0_.user_id user_id15_16_3_, user1_.id id1_26_0_, user1_.created_by created_2_26_0_, user1_.created_date created_3_26_0_, user1_.modified_by modified4_26_0_, user1_.modified_date modified5_26_0_, user1_.version version6_26_0_, user1_.email email7_26_0_, user1_.facebook_id facebook8_26_0_, user1_.hash_code hash_cod9_26_0_, user1_.linkedin_id linkedi10_26_0_, user1_.password passwor11_26_0_, user1_.role_id role_id15_26_0_, user1_.status status12_26_0_, user1_.twitter_id twitter13_26_0_, user1_.username usernam14_26_0_, role2_.id id1_22_1_, role2_.created_by created_2_22_1_, role2_.created_date created_3_22_1_, role2_.modified_by modified4_22_1_, role2_.modified_date modified5_22_1_, role2_.version version6_22_1_, role2_.description descript7_22_1_, role2_.name name8_22_1_, usersettin3_.id id1_27_2_, usersettin3_.created_by created_2_27_2_, usersettin3_.created_date created_3_27_2_, usersettin3_.modified_by modified4_27_2_, usersettin3_.modified_date modified5_27_2_, usersettin3_.version version6_27_2_, usersettin3_.last_news_send_date last_new7_27_2_, usersettin3_.preferred_language preferre8_27_2_, usersettin3_.recieve_newsletter recieve_9_27_2_, usersettin3_.user_id user_id10_27_2_ job_seeker jobseeker0_ left outer join user_auth user1_ on jobseeker0_.user_id=user1_.id left outer join role role2_ on user1_.role_id=role2_.id left outer join user_settings usersettin3_ on user1_.id=usersettin3_.user_id jobseeker0_.user_id=? hibernate: select jobseeker0_.id id1_16_3_, jobseeker0_.created_by created_2_16_3_, jobseeker0_.created_date created_3_16_3_, jobseeker0_.modified_by modified4_16_3_, jobseeker0_.modified_date modified5_16_3_, jobseeker0_.version version6_16_3_, jobseeker0_.adress adress7_16_3_, jobseeker0_.city city8_16_3_, jobseeker0_.forgot_password forgot_p9_16_3_, jobseeker0_.time_forgot_password time_fo10_16_3_, jobseeker0_.name name11_16_3_, jobseeker0_.not_seen not_see12_16_3_, jobseeker0_.phone phone13_16_3_, jobseeker0_.picture_path picture14_16_3_, jobseeker0_.user_id user_id15_16_3_, user1_.id id1_26_0_, user1_.created_by created_2_26_0_, user1_.created_date created_3_26_0_, user1_.modified_by modified4_26_0_, user1_.modified_date modified5_26_0_, user1_.version version6_26_0_, user1_.email email7_26_0_, user1_.facebook_id facebook8_26_0_, user1_.hash_code hash_cod9_26_0_, user1_.linkedin_id linkedi10_26_0_, user1_.password passwor11_26_0_, user1_.role_id role_id15_26_0_, user1_.status status12_26_0_, user1_.twitter_id twitter13_26_0_, user1_.username usernam14_26_0_, role2_.id id1_22_1_, role2_.created_by created_2_22_1_, role2_.created_date created_3_22_1_, role2_.modified_by modified4_22_1_, role2_.modified_date modified5_22_1_, role2_.version version6_22_1_, role2_.description descript7_22_1_, role2_.name name8_22_1_, usersettin3_.id id1_27_2_, usersettin3_.created_by created_2_27_2_, usersettin3_.created_date created_3_27_2_, usersettin3_.modified_by modified4_27_2_, usersettin3_.modified_date modified5_27_2_, usersettin3_.version version6_27_2_, usersettin3_.last_news_send_date last_new7_27_2_, usersettin3_.preferred_language preferre8_27_2_, usersettin3_.recieve_newsletter recieve_9_27_2_, usersettin3_.user_id user_id10_27_2_ job_seeker jobseeker0_ left outer join user_auth user1_ on jobseeker0_.user_id=user1_.id left outer join role role2_ on user1_.role_id=role2_.id left outer join user_settings usersettin3_ on user1_.id=usersettin3_.user_id jobseeker0_.user_id=? . . 

i tried put in jobseeker class @onetoone(fetch = fetchtype.lazy) write 1 query, example in findone() gives no proxy session user, did not load user , need it. in findall need jobseeker without user. write select hibernate.query need criteria. can me?

update: generic class:

public abstract class abstracthibernatedao<t extends baseentity, pk extends serializable> implements ioperations<t, pk> {      private class<t> clazz;     private class<pk> pkclazz;      @autowired     private sessionfactory sessionfactory;       protected final void setclazz(final class<t> clazztoset, final class<pk> plclazztoset) {         clazz = preconditions.checknotnull(clazztoset);         pkclazz = preconditions.checknotnull(plclazztoset);     }      @override     public final t findone(final pk id) {         return (t) getcurrentsession().get(clazz, id);     } 

update if fetchtype lazy goes on findall writes 1 query

@onetoone(fetch = fetchtype.lazy, optional = false) @joincolumn(name = "user_id") private user user; 

but gives error on jobseekerservice.findone(id) :

severe: servlet.service() servlet [spring-mvc-dispatcher] in context path [/proiectname] threw exception [request processing failed; nested exception org.apache.tiles.request.render.cannotrenderexception: servletexception including path '/web-inf/views/layout/adminlayout.jsp'.] root cause org.hibernate.lazyinitializationexception: not initialize proxy - no session     @ org.hibernate.proxy.abstractlazyinitializer.initialize(abstractlazyinitializer.java:165)     @ org.hibernate.proxy.abstractlazyinitializer.getimplementation(abstractlazyinitializer.java:286)     @ org.hibernate.proxy.pojo.javassist.javassistlazyinitializer.invoke(javassistlazyinitializer.java:185)     @ com.proiect.hrms.persistence.model.user_$$_jvstd45_a.getemail(user_$$_jvstd45_a.java)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:39)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25)     @ java.lang.reflect.method.invoke(method.java:597)     @ javax.el.beanelresolver.getvalue(beanelresolver.java:87)     @ org.apache.jasper.el.jasperelresolver.getvalue(jasperelresolver.java:104)     @ org.apache.el.parser.astvalue.getvalue(astvalue.java:183)     @ org.apache.el.valueexpressionimpl.getvalue(valueexpressionimpl.java:185)     @ org.apache.jasper.runtime.pagecontextimpl.proprietaryevaluate(pagecontextimpl.java:1026)     @ org.apache.jsp.web_002dinf.views.admin.main.candidates.details_jsp._jspservice(details_jsp.java:143)     @ org.apache.jasper.runtime.httpjspbase.service(httpjspbase.java:70)     @ javax.servlet.http.httpservlet.service(httpservlet.java:728)     @ org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:432)     @ org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:390)     @ org.apache.jasper.servlet.jspservlet.service(jspservlet.java:334)     @ javax.servlet.http.httpservlet.service(httpservlet.java:728)     @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305)     @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210)     @ org.apache.catalina.core.applicationdispatcher.invoke(applicationdispatcher.java:749)     @ org.apache.catalina.core.applicationdispatcher.doinclude(applicationdispatcher.java:605)     @ org.apache.catalina.core.applicationdispatcher.include(applicationdispatcher.java:544)     @ org.apache.jasper.runtime.jspruntimelibrary.include(jspruntimelibrary.java:954)     @ org.apache.jasper.runtime.pagecontextimpl.doinclude(pagecontextimpl.java:684)     @ org.apache.jasper.runtime.pagecontextimpl.include(pagecontextimpl.java:678)     @ org.apache.tiles.request.jsp.jsprequest.doinclude(jsprequest.java:123)     @ org.apache.tiles.request.abstractviewrequest.dispatch(abstractviewrequest.java:47)     @ org.apache.tiles.request.render.dispatchrenderer.render(dispatchrenderer.java:47)     @ org.apache.tiles.request.render.chaineddelegaterenderer.render(chaineddelegaterenderer.java:68)     @ org.apache.tiles.impl.basictilescontainer.render(basictilescontainer.java:259)     @ org.apache.tiles.template.insertattributemodel.renderattribute(insertattributemodel.java:188)     @ org.apache.tiles.template.insertattributemodel.execute(insertattributemodel.java:132)     @ org.apache.tiles.jsp.taglib.insertattributetag.dotag(insertattributetag.java:299)     @ org.apache.jsp.web_002dinf.views.layout.adminlayout_jsp._jspx_meth_tiles_005finsertattribute_005f3(adminlayout_jsp.java:304)     @ org.apache.jsp.web_002dinf.views.layout.adminlayout_jsp._jspservice(adminlayout_jsp.java:187)     @ org.apache.jasper.runtime.httpjspbase.service(httpjspbase.java:70)     @ javax.servlet.http.httpservlet.service(httpservlet.java:728)     @ org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:432)     @ org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:390)     @ org.apache.jasper.servlet.jspservlet.service(jspservlet.java:334)     @ javax.servlet.http.httpservlet.service(httpservlet.java:728)     @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305)     @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210)     @ org.apache.catalina.core.applicationdispatcher.invoke(applicationdispatcher.java:749)     @ org.apache.catalina.core.applicationdispatcher.processrequest(applicationdispatcher.java:487)     @ org.apache.catalina.core.applicationdispatcher.doforward(applicationdispatcher.java:412)     @ org.apache.catalina.core.applicationdispatcher.forward(applicationdispatcher.java:339)     @ org.apache.tiles.request.servlet.servletrequest.forward(servletrequest.java:265)     @ org.apache.tiles.request.servlet.servletrequest.doforward(servletrequest.java:228)     @ org.apache.tiles.request.abstractclientrequest.dispatch(abstractclientrequest.java:57)     @ org.apache.tiles.request.render.dispatchrenderer.render(dispatchrenderer.java:47)     @ org.apache.tiles.impl.basictilescontainer.render(basictilescontainer.java:259)     @ org.apache.tiles.impl.basictilescontainer.render(basictilescontainer.java:397)     @ org.apache.tiles.impl.basictilescontainer.render(basictilescontainer.java:238)     @ org.apache.tiles.impl.basictilescontainer.render(basictilescontainer.java:221)     @ org.apache.tiles.renderer.definitionrenderer.render(definitionrenderer.java:59)     @ org.springframework.web.servlet.view.tiles3.tilesview.rendermergedoutputmodel(tilesview.java:114)     @ org.springframework.web.servlet.view.abstractview.render(abstractview.java:303)     @ org.springframework.web.servlet.dispatcherservlet.render(dispatcherservlet.java:1228)     @ org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:1011)     @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:955)     @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:877)     @ 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:621)     @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:842)     @ javax.servlet.http.httpservlet.service(httpservlet.java:728)     @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305)     @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210)     @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330)     @ org.springframework.security.web.access.intercept.filtersecurityinterceptor.invoke(filtersecurityinterceptor.java:118)     @ org.springframework.security.web.access.intercept.filtersecurityinterceptor.dofilter(filtersecurityinterceptor.java:84)     @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342)     @ org.springframework.security.web.access.exceptiontranslationfilter.dofilter(exceptiontranslationfilter.java:113)     @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342)     @ org.springframework.security.web.session.sessionmanagementfilter.dofilter(sessionmanagementfilter.java:103)     @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342)     @ org.springframework.security.web.authentication.anonymousauthenticationfilter.dofilter(anonymousauthenticationfilter.java:113)     @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342)     @ org.springframework.security.web.servletapi.securitycontextholderawarerequestfilter.dofilter(securitycontextholderawarerequestfilter.java:154)     @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342)     @ org.springframework.security.web.savedrequest.requestcacheawarefilter.dofilter(requestcacheawarefilter.java:45)     @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342)     @ org.springframework.security.web.authentication.abstractauthenticationprocessingfilter.dofilter(abstractauthenticationprocessingfilter.java:199)     @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342)     @ org.springframework.security.web.authentication.logout.logoutfilter.dofilter(logoutfilter.java:110)     @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342)     @ org.springframework.security.web.context.request.async.webasyncmanagerintegrationfilter.dofilterinternal(webasyncmanagerintegrationfilter.java:50)     @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107)     @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342)     @ org.springframework.security.web.context.securitycontextpersistencefilter.dofilter(securitycontextpersistencefilter.java:87)     @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342)     @ org.springframework.security.web.filterchainproxy.dofilterinternal(filterchainproxy.java:192)     @ org.springframework.security.web.filterchainproxy.dofilter(filterchainproxy.java:160)     @ org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:344)     @ org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:261)     @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243)     @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210)     @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222)     @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123)     @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:472)     @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171)     @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:99)     @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:936)     @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118)     @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:407)     @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1004)     @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:589)     @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:312)     @ java.util.concurrent.threadpoolexecutor$worker.runtask(threadpoolexecutor.java:886)     @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:908)     @ java.lang.thread.run(thread.java:662) 

just replace specific piece of code following code , done.

@onetoone(mappedby = "user", fetch = fetchtype.lazy, optional = false, cascade=cascadetype.all)  @fetch(fetchmode.join)  private jobseeker jobseeker; 

see, have changed fetchmode.join instead of fetchmode.select.

from hibernate docs,

select : use select each individual entity, collection, or join load

for more details please refer : hibernate docs.

update : default hibernate's behavior fetching strategy playing role here,

onetomany: lazy manytoone: eager manytomany: lazy onetoone: eager 

to overcome need update onetoone mapping in jobseaker class this,

@onetoone(fetch = fetchtype.lazy, optional = false) @joincolumn(name = "user_id") private user user; 

refer : use of optional flag.

update : have disabled default behavior of eager loading of hibernate. need trigger hibernate load data using join query this,

session session = getcurrentsession();   criteria criteria = session.createcriteria(jobseeker.class, "jobseeker")         .createalias("jobseeker.user","user") //default inner join         .setfirstresult(datatablefilter.idisplaystart)         .setmaxresults(datatablefilter.idisplaylength); list<?> list = new arraylist(); list = criteria.list(); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -