[英]Hibernate Criteria unable to retrieve collection of a nested object
I have got a TeamPlayer
object with a Player
object as on of its attribute. 我有一个
TeamPlayer
对象,其属性上有一个Player
对象。 The Player
object has One-to-Many
relation with PlayerSuspension
. 该
Player
对象有One-to-Many
有关系PlayerSuspension
。
When I try to retrieve all TeamPlayers
and fetch all player's suspensions along with. 当我尝试检索所有
TeamPlayers
并获取所有玩家的停赛TeamPlayers
。 It throws following error. 它引发以下错误。 Could you help me out where's the issue?
您能帮我解决哪里出问题吗?
org.hibernate.QueryException: could not resolve property: ps of: we.data.weref.model.TeamPlayer
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:62)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:56)
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1770)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1745)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:510)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:525)
at org.hibernate.criterion.NullExpression.toSqlString(NullExpression.java:36)
at org.hibernate.criterion.LogicalExpression.toSqlString(LogicalExpression.java:42)
at org.hibernate.criterion.LogicalExpression.toSqlString(LogicalExpression.java:42)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:400)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:106)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:75)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:80)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1760)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
at we.data.weref.dao.hbn.TeamDaoHbn.getTeamPlayers(TeamDaoHbn.java:109)
at we.data.weref.service.impl.TeamServiceImpl.getTeamPlayers(TeamServiceImpl.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:68)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy169.getTeamPlayers(Unknown Source)
at we.data.weref.controller.api.TeamControllerApi.getTeamPlayers(TeamControllerApi.java:71)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
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.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.www.DigestAuthenticationFilter.doFilter(DigestAuthenticationFilter.java:233)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
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:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
My hibernate criteria is as following 我的休眠标准如下
return getSession()
.createCriteria(TeamPlayer.class, "tp")
.createAlias("player.playerSuspensions", "ps", JoinType.LEFT_OUTER_JOIN)
.add(Restrictions.and(
Restrictions.or(
Restrictions.isNull("ps"),
Restrictions.eq("ps.team.id", teamId)
),
Restrictions.eq("tp.team.id", teamId)
)
)
.list();
@Entity
public class TeamPlayer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne
@JsonSerialize(using = IdSerializer.class)
private Team team;
@ManyToOne
@JsonSerialize(using = IdFirstNameLastNameSerializer.class)
private Player player;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
}
@Entity
public class Player implements Serializable {
private static final long serialVersionUID = 5688361063269574977L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@OneToMany(mappedBy = "player")
private List<PlayerSuspension> playerSuspensions;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
@Entity
public class PlayerSuspension implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@ManyToOne
private Player player;
@ManyToOne
private Team team;
@ManyToOne // if competition == null then player is suspended from all the competitions
private Competition competition; // if competition given then only from that particular competition
private Date startDate;
private Integer games;
private Integer weeks;
private Integer gamesSpent;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
}
ps
is an alias. ps
是别名。 You should specify a property name of PlayerSuspension
. 您应该指定
PlayerSuspension
的属性名称。 For an example ps.name
. 例如
ps.name
。
You can use something like this (you can convert it to criteria). 您可以使用类似的东西(可以将其转换为条件)。 Maybe, it can be more simply
也许可以更简单
from TeamPlayer teamPlayer
left join teamPlayer.player player
left join player.playerSuspensions suspension
where suspension.team.id = :teamId
or not exists (select innerTeamPlayer.id from TeamPlayer innerTeamPlayer
inner join innerTeamPlayer.player innerPlayer
inner join innerPlayer.playerSuspensions
where innerTeamPlayer.id = teamPlayer.id)
Update 更新资料
You can't use TeamPlayer.getPlayerSuspension()
with result of this HQL. 您不能将
TeamPlayer.getPlayerSuspension()
与此HQL一起使用。 Because of collections are lazy by default. 由于默认情况下集合是惰性的。 You can try to fetch
playerSuspensions
by HQL with left join fetch
您可以尝试通过
left join fetch
playerSuspensions
获取通过HQL提取playerSuspensions
from TeamPlayer teamPlayer
left join teamPlayer.player player
left join fetch player.playerSuspensions suspension
where suspension.team.id = :teamId
or not exists (select innerTeamPlayer.id from TeamPlayer innerTeamPlayer
inner join innerTeamPlayer.player innerPlayer
inner join innerPlayer.playerSuspensions
where innerTeamPlayer.id = teamPlayer.id)
or you can change a mapping 或者您可以更改映射
@OneToMany(fetch = FetchType.EAGER, mappedBy = "player")
private List<PlayerSuspension> playerSuspensions;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.