Hibernate 为什么创建 n 个查询而不是 1 个?
Hibernate why is creating n query instead of 1?
我想用一个查询和条件从 JobSeeker table 加载所有 jobSeekers,休眠而不是编写 1 个查询,而是为 n 个 JobSeeker 编写 n 个查询,我不知道为什么请帮忙.
这是我的求职者 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;
.
.
.
这是我的用户 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;
.
.
.
和用户设置class:
@Entity
@Table(name="USER_SETTINGS")
public class UserSettings extends BaseEntity{
@OneToOne
@JoinColumn(name = "user_id")
@Expose
private User user;
.
.
.
还有我的 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();
它会查询:
Hibernate: select this_.ID as ID1_16_3_, this_.CREATED_BY as CREATED_2_16_3_, this_.CREATED_DATE as CREATED_3_16_3_, this_.MODIFIED_BY as MODIFIED4_16_3_, this_.MODIFIED_DATE as MODIFIED5_16_3_, this_.VERSION as VERSION6_16_3_, this_.adress as adress7_16_3_, this_.city as city8_16_3_, this_.forgot_password as forgot_p9_16_3_, this_.time_forgot_password as time_fo10_16_3_, this_.name as name11_16_3_, this_.not_seen as not_see12_16_3_, this_.phone as phone13_16_3_, this_.picture_path as picture14_16_3_, this_.user_id as user_id15_16_3_, user2_.ID as ID1_26_0_, user2_.CREATED_BY as CREATED_2_26_0_, user2_.CREATED_DATE as CREATED_3_26_0_, user2_.MODIFIED_BY as MODIFIED4_26_0_, user2_.MODIFIED_DATE as MODIFIED5_26_0_, user2_.VERSION as VERSION6_26_0_, user2_.email as email7_26_0_, user2_.facebook_id as facebook8_26_0_, user2_.hash_code as hash_cod9_26_0_, user2_.linkedin_id as linkedi10_26_0_, user2_.password as passwor11_26_0_, user2_.role_id as role_id15_26_0_, user2_.status as status12_26_0_, user2_.twitter_id as twitter13_26_0_, user2_.username as usernam14_26_0_, role3_.ID as ID1_22_1_, role3_.CREATED_BY as CREATED_2_22_1_, role3_.CREATED_DATE as CREATED_3_22_1_, role3_.MODIFIED_BY as MODIFIED4_22_1_, role3_.MODIFIED_DATE as MODIFIED5_22_1_, role3_.VERSION as VERSION6_22_1_, role3_.description as descript7_22_1_, role3_.name as name8_22_1_, usersettin4_.ID as ID1_27_2_, usersettin4_.CREATED_BY as CREATED_2_27_2_, usersettin4_.CREATED_DATE as CREATED_3_27_2_, usersettin4_.MODIFIED_BY as MODIFIED4_27_2_, usersettin4_.MODIFIED_DATE as MODIFIED5_27_2_, usersettin4_.VERSION as VERSION6_27_2_, usersettin4_.last_news_send_date as last_new7_27_2_, usersettin4_.preferred_language as preferre8_27_2_, usersettin4_.recieve_newsletter as recieve_9_27_2_, usersettin4_.user_id as user_id10_27_2_ from 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 by this_.name asc
Hibernate: select jobseeker0_.ID as ID1_16_3_, jobseeker0_.CREATED_BY as CREATED_2_16_3_, jobseeker0_.CREATED_DATE as CREATED_3_16_3_, jobseeker0_.MODIFIED_BY as MODIFIED4_16_3_, jobseeker0_.MODIFIED_DATE as MODIFIED5_16_3_, jobseeker0_.VERSION as VERSION6_16_3_, jobseeker0_.adress as adress7_16_3_, jobseeker0_.city as city8_16_3_, jobseeker0_.forgot_password as forgot_p9_16_3_, jobseeker0_.time_forgot_password as time_fo10_16_3_, jobseeker0_.name as name11_16_3_, jobseeker0_.not_seen as not_see12_16_3_, jobseeker0_.phone as phone13_16_3_, jobseeker0_.picture_path as picture14_16_3_, jobseeker0_.user_id as user_id15_16_3_, user1_.ID as ID1_26_0_, user1_.CREATED_BY as CREATED_2_26_0_, user1_.CREATED_DATE as CREATED_3_26_0_, user1_.MODIFIED_BY as MODIFIED4_26_0_, user1_.MODIFIED_DATE as MODIFIED5_26_0_, user1_.VERSION as VERSION6_26_0_, user1_.email as email7_26_0_, user1_.facebook_id as facebook8_26_0_, user1_.hash_code as hash_cod9_26_0_, user1_.linkedin_id as linkedi10_26_0_, user1_.password as passwor11_26_0_, user1_.role_id as role_id15_26_0_, user1_.status as status12_26_0_, user1_.twitter_id as twitter13_26_0_, user1_.username as usernam14_26_0_, role2_.ID as ID1_22_1_, role2_.CREATED_BY as CREATED_2_22_1_, role2_.CREATED_DATE as CREATED_3_22_1_, role2_.MODIFIED_BY as MODIFIED4_22_1_, role2_.MODIFIED_DATE as MODIFIED5_22_1_, role2_.VERSION as VERSION6_22_1_, role2_.description as descript7_22_1_, role2_.name as name8_22_1_, usersettin3_.ID as ID1_27_2_, usersettin3_.CREATED_BY as CREATED_2_27_2_, usersettin3_.CREATED_DATE as CREATED_3_27_2_, usersettin3_.MODIFIED_BY as MODIFIED4_27_2_, usersettin3_.MODIFIED_DATE as MODIFIED5_27_2_, usersettin3_.VERSION as VERSION6_27_2_, usersettin3_.last_news_send_date as last_new7_27_2_, usersettin3_.preferred_language as preferre8_27_2_, usersettin3_.recieve_newsletter as recieve_9_27_2_, usersettin3_.user_id as user_id10_27_2_ from 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 where jobseeker0_.user_id=?
Hibernate: select jobseeker0_.ID as ID1_16_3_, jobseeker0_.CREATED_BY as CREATED_2_16_3_, jobseeker0_.CREATED_DATE as CREATED_3_16_3_, jobseeker0_.MODIFIED_BY as MODIFIED4_16_3_, jobseeker0_.MODIFIED_DATE as MODIFIED5_16_3_, jobseeker0_.VERSION as VERSION6_16_3_, jobseeker0_.adress as adress7_16_3_, jobseeker0_.city as city8_16_3_, jobseeker0_.forgot_password as forgot_p9_16_3_, jobseeker0_.time_forgot_password as time_fo10_16_3_, jobseeker0_.name as name11_16_3_, jobseeker0_.not_seen as not_see12_16_3_, jobseeker0_.phone as phone13_16_3_, jobseeker0_.picture_path as picture14_16_3_, jobseeker0_.user_id as user_id15_16_3_, user1_.ID as ID1_26_0_, user1_.CREATED_BY as CREATED_2_26_0_, user1_.CREATED_DATE as CREATED_3_26_0_, user1_.MODIFIED_BY as MODIFIED4_26_0_, user1_.MODIFIED_DATE as MODIFIED5_26_0_, user1_.VERSION as VERSION6_26_0_, user1_.email as email7_26_0_, user1_.facebook_id as facebook8_26_0_, user1_.hash_code as hash_cod9_26_0_, user1_.linkedin_id as linkedi10_26_0_, user1_.password as passwor11_26_0_, user1_.role_id as role_id15_26_0_, user1_.status as status12_26_0_, user1_.twitter_id as twitter13_26_0_, user1_.username as usernam14_26_0_, role2_.ID as ID1_22_1_, role2_.CREATED_BY as CREATED_2_22_1_, role2_.CREATED_DATE as CREATED_3_22_1_, role2_.MODIFIED_BY as MODIFIED4_22_1_, role2_.MODIFIED_DATE as MODIFIED5_22_1_, role2_.VERSION as VERSION6_22_1_, role2_.description as descript7_22_1_, role2_.name as name8_22_1_, usersettin3_.ID as ID1_27_2_, usersettin3_.CREATED_BY as CREATED_2_27_2_, usersettin3_.CREATED_DATE as CREATED_3_27_2_, usersettin3_.MODIFIED_BY as MODIFIED4_27_2_, usersettin3_.MODIFIED_DATE as MODIFIED5_27_2_, usersettin3_.VERSION as VERSION6_27_2_, usersettin3_.last_news_send_date as last_new7_27_2_, usersettin3_.preferred_language as preferre8_27_2_, usersettin3_.recieve_newsletter as recieve_9_27_2_, usersettin3_.user_id as user_id10_27_2_ from 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 where jobseeker0_.user_id=?
.
.
我试图在 JobSeeker class @OneToOne(fetch = FetchType.LAZY) 中写入一个查询,但是例如在 findOne() 中没有为用户提供代理会话,没有加载用户,我需要它。在 findAll 中,我只需要没有用户的 JobSeeker。我会用 hibernate.query 写 select 但我需要处理标准。谁能帮帮我?
更新:
通用 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);
}
更新
如果 fetchType 懒惰,它在 findAll 上运行良好,只写一个查询
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id")
private User user;
但在 jobSeekerService.findOne(id) 上给出此错误:
SEVERE: Servlet.service() for servlet [spring-mvc-dispatcher] in context with path [/ProiectName] threw exception [Request processing failed; nested exception is org.apache.tiles.request.render.CannotRenderException: ServletException including path '/WEB-INF/views/layout/adminLayout.jsp'.] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
at com.proiect.hrms.persistence.model.User_$$_jvstd45_a.getEmail(User_$$_jvstd45_a.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
at org.apache.jsp.WEB_002dINF.views.admin.main.candidates.details_jsp._jspService(details_jsp.java:143)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:605)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:544)
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
at org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684)
at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:678)
at org.apache.tiles.request.jsp.JspRequest.doInclude(JspRequest.java:123)
at org.apache.tiles.request.AbstractViewRequest.dispatch(AbstractViewRequest.java:47)
at org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:47)
at org.apache.tiles.request.render.ChainedDelegateRenderer.render(ChainedDelegateRenderer.java:68)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
at org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:188)
at org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:132)
at org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299)
at org.apache.jsp.WEB_002dINF.views.layout.adminLayout_jsp._jspx_meth_tiles_005finsertAttribute_005f3(adminLayout_jsp.java:304)
at org.apache.jsp.WEB_002dINF.views.layout.adminLayout_jsp._jspService(adminLayout_jsp.java:187)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at org.apache.tiles.request.servlet.ServletRequest.forward(ServletRequest.java:265)
at org.apache.tiles.request.servlet.ServletRequest.doForward(ServletRequest.java:228)
at org.apache.tiles.request.AbstractClientRequest.dispatch(AbstractClientRequest.java:57)
at org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:47)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:397)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:238)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:221)
at org.apache.tiles.renderer.DefinitionRenderer.render(DefinitionRenderer.java:59)
at org.springframework.web.servlet.view.tiles3.TilesView.renderMergedOutputModel(TilesView.java:114)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
只需将您的特定代码替换为以下代码即可。
@OneToOne(mappedBy = "user", fetch = FetchType.LAZY, optional = false, cascade=CascadeType.ALL)
@Fetch(FetchMode.JOIN)
private JobSeeker jobSeeker;
看,我刚刚把FetchMode.JOIN改成了FetchMode.SELECT.
来自 Hibernate 文档,
SELECT : use a select for each individual entity, collection, or join load
详情请参考:Hibernate Docs.
UPDATE :默认情况下,hibernate 的行为是这样的获取策略,这也在此处发挥作用,
OneToMany: LAZY
ManyToOne: EAGER
ManyToMany: LAZY
OneToOne: EAGER
要克服这个问题,您只需像这样更新 JobSeaker class 中的 OneToOne 映射,
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id")
private User user;
参考:Use of Optional flag.
UPDATE :现在我们已经禁用了预先加载休眠的默认行为。所以我们需要像这样使用连接查询来触发hibernate来加载数据,
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();
我想用一个查询和条件从 JobSeeker table 加载所有 jobSeekers,休眠而不是编写 1 个查询,而是为 n 个 JobSeeker 编写 n 个查询,我不知道为什么请帮忙. 这是我的求职者 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;
.
.
.
这是我的用户 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;
.
.
.
和用户设置class:
@Entity
@Table(name="USER_SETTINGS")
public class UserSettings extends BaseEntity{
@OneToOne
@JoinColumn(name = "user_id")
@Expose
private User user;
.
.
.
还有我的 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();
它会查询:
Hibernate: select this_.ID as ID1_16_3_, this_.CREATED_BY as CREATED_2_16_3_, this_.CREATED_DATE as CREATED_3_16_3_, this_.MODIFIED_BY as MODIFIED4_16_3_, this_.MODIFIED_DATE as MODIFIED5_16_3_, this_.VERSION as VERSION6_16_3_, this_.adress as adress7_16_3_, this_.city as city8_16_3_, this_.forgot_password as forgot_p9_16_3_, this_.time_forgot_password as time_fo10_16_3_, this_.name as name11_16_3_, this_.not_seen as not_see12_16_3_, this_.phone as phone13_16_3_, this_.picture_path as picture14_16_3_, this_.user_id as user_id15_16_3_, user2_.ID as ID1_26_0_, user2_.CREATED_BY as CREATED_2_26_0_, user2_.CREATED_DATE as CREATED_3_26_0_, user2_.MODIFIED_BY as MODIFIED4_26_0_, user2_.MODIFIED_DATE as MODIFIED5_26_0_, user2_.VERSION as VERSION6_26_0_, user2_.email as email7_26_0_, user2_.facebook_id as facebook8_26_0_, user2_.hash_code as hash_cod9_26_0_, user2_.linkedin_id as linkedi10_26_0_, user2_.password as passwor11_26_0_, user2_.role_id as role_id15_26_0_, user2_.status as status12_26_0_, user2_.twitter_id as twitter13_26_0_, user2_.username as usernam14_26_0_, role3_.ID as ID1_22_1_, role3_.CREATED_BY as CREATED_2_22_1_, role3_.CREATED_DATE as CREATED_3_22_1_, role3_.MODIFIED_BY as MODIFIED4_22_1_, role3_.MODIFIED_DATE as MODIFIED5_22_1_, role3_.VERSION as VERSION6_22_1_, role3_.description as descript7_22_1_, role3_.name as name8_22_1_, usersettin4_.ID as ID1_27_2_, usersettin4_.CREATED_BY as CREATED_2_27_2_, usersettin4_.CREATED_DATE as CREATED_3_27_2_, usersettin4_.MODIFIED_BY as MODIFIED4_27_2_, usersettin4_.MODIFIED_DATE as MODIFIED5_27_2_, usersettin4_.VERSION as VERSION6_27_2_, usersettin4_.last_news_send_date as last_new7_27_2_, usersettin4_.preferred_language as preferre8_27_2_, usersettin4_.recieve_newsletter as recieve_9_27_2_, usersettin4_.user_id as user_id10_27_2_ from 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 by this_.name asc
Hibernate: select jobseeker0_.ID as ID1_16_3_, jobseeker0_.CREATED_BY as CREATED_2_16_3_, jobseeker0_.CREATED_DATE as CREATED_3_16_3_, jobseeker0_.MODIFIED_BY as MODIFIED4_16_3_, jobseeker0_.MODIFIED_DATE as MODIFIED5_16_3_, jobseeker0_.VERSION as VERSION6_16_3_, jobseeker0_.adress as adress7_16_3_, jobseeker0_.city as city8_16_3_, jobseeker0_.forgot_password as forgot_p9_16_3_, jobseeker0_.time_forgot_password as time_fo10_16_3_, jobseeker0_.name as name11_16_3_, jobseeker0_.not_seen as not_see12_16_3_, jobseeker0_.phone as phone13_16_3_, jobseeker0_.picture_path as picture14_16_3_, jobseeker0_.user_id as user_id15_16_3_, user1_.ID as ID1_26_0_, user1_.CREATED_BY as CREATED_2_26_0_, user1_.CREATED_DATE as CREATED_3_26_0_, user1_.MODIFIED_BY as MODIFIED4_26_0_, user1_.MODIFIED_DATE as MODIFIED5_26_0_, user1_.VERSION as VERSION6_26_0_, user1_.email as email7_26_0_, user1_.facebook_id as facebook8_26_0_, user1_.hash_code as hash_cod9_26_0_, user1_.linkedin_id as linkedi10_26_0_, user1_.password as passwor11_26_0_, user1_.role_id as role_id15_26_0_, user1_.status as status12_26_0_, user1_.twitter_id as twitter13_26_0_, user1_.username as usernam14_26_0_, role2_.ID as ID1_22_1_, role2_.CREATED_BY as CREATED_2_22_1_, role2_.CREATED_DATE as CREATED_3_22_1_, role2_.MODIFIED_BY as MODIFIED4_22_1_, role2_.MODIFIED_DATE as MODIFIED5_22_1_, role2_.VERSION as VERSION6_22_1_, role2_.description as descript7_22_1_, role2_.name as name8_22_1_, usersettin3_.ID as ID1_27_2_, usersettin3_.CREATED_BY as CREATED_2_27_2_, usersettin3_.CREATED_DATE as CREATED_3_27_2_, usersettin3_.MODIFIED_BY as MODIFIED4_27_2_, usersettin3_.MODIFIED_DATE as MODIFIED5_27_2_, usersettin3_.VERSION as VERSION6_27_2_, usersettin3_.last_news_send_date as last_new7_27_2_, usersettin3_.preferred_language as preferre8_27_2_, usersettin3_.recieve_newsletter as recieve_9_27_2_, usersettin3_.user_id as user_id10_27_2_ from 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 where jobseeker0_.user_id=?
Hibernate: select jobseeker0_.ID as ID1_16_3_, jobseeker0_.CREATED_BY as CREATED_2_16_3_, jobseeker0_.CREATED_DATE as CREATED_3_16_3_, jobseeker0_.MODIFIED_BY as MODIFIED4_16_3_, jobseeker0_.MODIFIED_DATE as MODIFIED5_16_3_, jobseeker0_.VERSION as VERSION6_16_3_, jobseeker0_.adress as adress7_16_3_, jobseeker0_.city as city8_16_3_, jobseeker0_.forgot_password as forgot_p9_16_3_, jobseeker0_.time_forgot_password as time_fo10_16_3_, jobseeker0_.name as name11_16_3_, jobseeker0_.not_seen as not_see12_16_3_, jobseeker0_.phone as phone13_16_3_, jobseeker0_.picture_path as picture14_16_3_, jobseeker0_.user_id as user_id15_16_3_, user1_.ID as ID1_26_0_, user1_.CREATED_BY as CREATED_2_26_0_, user1_.CREATED_DATE as CREATED_3_26_0_, user1_.MODIFIED_BY as MODIFIED4_26_0_, user1_.MODIFIED_DATE as MODIFIED5_26_0_, user1_.VERSION as VERSION6_26_0_, user1_.email as email7_26_0_, user1_.facebook_id as facebook8_26_0_, user1_.hash_code as hash_cod9_26_0_, user1_.linkedin_id as linkedi10_26_0_, user1_.password as passwor11_26_0_, user1_.role_id as role_id15_26_0_, user1_.status as status12_26_0_, user1_.twitter_id as twitter13_26_0_, user1_.username as usernam14_26_0_, role2_.ID as ID1_22_1_, role2_.CREATED_BY as CREATED_2_22_1_, role2_.CREATED_DATE as CREATED_3_22_1_, role2_.MODIFIED_BY as MODIFIED4_22_1_, role2_.MODIFIED_DATE as MODIFIED5_22_1_, role2_.VERSION as VERSION6_22_1_, role2_.description as descript7_22_1_, role2_.name as name8_22_1_, usersettin3_.ID as ID1_27_2_, usersettin3_.CREATED_BY as CREATED_2_27_2_, usersettin3_.CREATED_DATE as CREATED_3_27_2_, usersettin3_.MODIFIED_BY as MODIFIED4_27_2_, usersettin3_.MODIFIED_DATE as MODIFIED5_27_2_, usersettin3_.VERSION as VERSION6_27_2_, usersettin3_.last_news_send_date as last_new7_27_2_, usersettin3_.preferred_language as preferre8_27_2_, usersettin3_.recieve_newsletter as recieve_9_27_2_, usersettin3_.user_id as user_id10_27_2_ from 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 where jobseeker0_.user_id=?
.
.
我试图在 JobSeeker class @OneToOne(fetch = FetchType.LAZY) 中写入一个查询,但是例如在 findOne() 中没有为用户提供代理会话,没有加载用户,我需要它。在 findAll 中,我只需要没有用户的 JobSeeker。我会用 hibernate.query 写 select 但我需要处理标准。谁能帮帮我?
更新: 通用 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);
}
更新 如果 fetchType 懒惰,它在 findAll 上运行良好,只写一个查询
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id")
private User user;
但在 jobSeekerService.findOne(id) 上给出此错误:
SEVERE: Servlet.service() for servlet [spring-mvc-dispatcher] in context with path [/ProiectName] threw exception [Request processing failed; nested exception is org.apache.tiles.request.render.CannotRenderException: ServletException including path '/WEB-INF/views/layout/adminLayout.jsp'.] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
at com.proiect.hrms.persistence.model.User_$$_jvstd45_a.getEmail(User_$$_jvstd45_a.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:87)
at org.apache.jasper.el.JasperELResolver.getValue(JasperELResolver.java:104)
at org.apache.el.parser.AstValue.getValue(AstValue.java:183)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:1026)
at org.apache.jsp.WEB_002dINF.views.admin.main.candidates.details_jsp._jspService(details_jsp.java:143)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:605)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:544)
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
at org.apache.jasper.runtime.PageContextImpl.doInclude(PageContextImpl.java:684)
at org.apache.jasper.runtime.PageContextImpl.include(PageContextImpl.java:678)
at org.apache.tiles.request.jsp.JspRequest.doInclude(JspRequest.java:123)
at org.apache.tiles.request.AbstractViewRequest.dispatch(AbstractViewRequest.java:47)
at org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:47)
at org.apache.tiles.request.render.ChainedDelegateRenderer.render(ChainedDelegateRenderer.java:68)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
at org.apache.tiles.template.InsertAttributeModel.renderAttribute(InsertAttributeModel.java:188)
at org.apache.tiles.template.InsertAttributeModel.execute(InsertAttributeModel.java:132)
at org.apache.tiles.jsp.taglib.InsertAttributeTag.doTag(InsertAttributeTag.java:299)
at org.apache.jsp.WEB_002dINF.views.layout.adminLayout_jsp._jspx_meth_tiles_005finsertAttribute_005f3(adminLayout_jsp.java:304)
at org.apache.jsp.WEB_002dINF.views.layout.adminLayout_jsp._jspService(adminLayout_jsp.java:187)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at org.apache.tiles.request.servlet.ServletRequest.forward(ServletRequest.java:265)
at org.apache.tiles.request.servlet.ServletRequest.doForward(ServletRequest.java:228)
at org.apache.tiles.request.AbstractClientRequest.dispatch(AbstractClientRequest.java:57)
at org.apache.tiles.request.render.DispatchRenderer.render(DispatchRenderer.java:47)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:259)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:397)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:238)
at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:221)
at org.apache.tiles.renderer.DefinitionRenderer.render(DefinitionRenderer.java:59)
at org.springframework.web.servlet.view.tiles3.TilesView.renderMergedOutputModel(TilesView.java:114)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
只需将您的特定代码替换为以下代码即可。
@OneToOne(mappedBy = "user", fetch = FetchType.LAZY, optional = false, cascade=CascadeType.ALL)
@Fetch(FetchMode.JOIN)
private JobSeeker jobSeeker;
看,我刚刚把FetchMode.JOIN改成了FetchMode.SELECT.
来自 Hibernate 文档,
SELECT : use a select for each individual entity, collection, or join load
详情请参考:Hibernate Docs.
UPDATE :默认情况下,hibernate 的行为是这样的获取策略,这也在此处发挥作用,
OneToMany: LAZY
ManyToOne: EAGER
ManyToMany: LAZY
OneToOne: EAGER
要克服这个问题,您只需像这样更新 JobSeaker class 中的 OneToOne 映射,
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id")
private User user;
参考:Use of Optional flag.
UPDATE :现在我们已经禁用了预先加载休眠的默认行为。所以我们需要像这样使用连接查询来触发hibernate来加载数据,
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();