How do I make a left join with criteria with a n n mapped table in java?

3

I tried to do this, however, but it is returning null in the query. Anyone have any ideas how I can resolve this?

 public List<Pessoa> filtrar(Pessoa pessoa) {
    Session session = sessionFactory.getCurrentSession();
    Criteria criteria = session.createCriteria(Pessoa.class);
    //criteria.createAlias("PESSOAXHABILIDADE", "PXH", Criteria.LEFT_JOIN);
    criteria.createAlias("com.br.bcb.negocio.Habilidade", "h", Criteria.LEFT_JOIN);
    Disjunction disjunction = Restrictions.disjunction();

    if (pessoa.getNome() != null) {
        disjunction.add(Restrictions.or(Restrictions.like("nome", pessoa.getNome(),MatchMode.ANYWHERE)));
    }

    if (pessoa.getSexo() != null) {
        disjunction.add(Restrictions.or(Restrictions.eq("sexo", pessoa.getSexo())));
    }

    if (pessoa.getIdade() != 0) {
        disjunction.add(Restrictions.or(Restrictions.eq("idade", pessoa.getIdade())));
    }

    if (pessoa.getNascimento() != null) {
        disjunction.add(Restrictions.or(Restrictions.eq("nascimento", pessoa.getNascimento())));
    }

    if (pessoa.getHabilidades() != null) {          
        for (Habilidade habilidade : pessoa.getHabilidades()) {
            disjunction.add(Restrictions.or(Restrictions.like("h.nome", habilidade.getNome(), MatchMode.ANYWHERE)));
        }
    }

    if (disjunction.conditions() != null && disjunction.conditions().iterator().hasNext()) {
        criteria.add(Restrictions.and(disjunction));
    }
    return criteria.list();
}
 Root cause:

org.hibernate.QueryException: could not resolve property: com of: com.br.bcb.negocio.Pessoa
     at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
     at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)
     at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1928)
     at org.hibernate.loader.criteria.EntityCriteriaInfoProvider.getType(EntityCriteriaInfoProvider.java:57)
     at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathInfo(CriteriaQueryTranslator.java:251)
     at org.hibernate.loader.criteria.CriteriaQueryTranslator.createCriteriaEntityNameMap(CriteriaQueryTranslator.java:230)
     at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:112)
     at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:86)
     at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1602)
     at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)
     at com.br.bcb.dao.PessoaDao.filtrar(PessoaDao.java:127)
     at com.br.bcb.mediador.pessoa.PessoaMediator.filtrar(PessoaMediator.java:150)
     at com.br.bcb.mediador.pessoa.PessoaMediator$$FastClassBySpringCGLIB$$c06a734.invoke(<generated>)
     at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
     at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:711)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
     at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
     at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
     at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
     at com.br.bcb.mediador.pessoa.PessoaMediator$$EnhancerBySpringCGLIB$$92453e52.filtrar(<generated>)
     at WICKET_com.br.bcb.mediador.pessoa.PessoaMediator$$FastClassByCGLIB$$c06a734.invoke(<generated>)
     at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
     at org.apache.wicket.proxy.LazyInitProxyFactory$CGLibInterceptor.intercept(LazyInitProxyFactory.java:317)
     at WICKET_com.br.bcb.mediador.pessoa.PessoaMediator$$EnhancerByCGLIB$$b6f83b22.filtrar(<generated>)
     at com.br.bcb.pages.pessoa.PessoaFiltroPage$2.onSubmit(PessoaFiltroPage.java:98)
     at org.apache.wicket.ajax.markup.html.form.AjaxButton$1.onSubmit(AjaxButton.java:101)
     at org.apache.wicket.ajax.form.AjaxFormSubmitBehavior.onEvent(AjaxFormSubmitBehavior.java:143)
     at org.apache.wicket.ajax.AjaxEventBehavior.respond(AjaxEventBehavior.java:177)
     at org.apache.wicket.ajax.AbstractDefaultAjaxBehavior.onRequest(AbstractDefaultAjaxBehavior.java:302)
     at org.apache.wicket.request.target.component.listener.BehaviorRequestTarget.processEvents(BehaviorRequestTarget.java:157)
     at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
     at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)
     at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
     at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1436)
     at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
     at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:486)
     at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:319)
     at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
     at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
     at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
     at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
     at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
     at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
     at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
     at org.mortbay.jetty.Server.handle(Server.java:326)
     at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
     at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:945)
     at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
     at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
     at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
     at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228)
     at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
    
asked by anonymous 10.02.2017 / 11:56

1 answer

1

Well, I just solved the problem, it was on the line where I create the by the way. BEFORE:

criteria.createAlias("com.br.bcb.negocio.Habilidade", "h", Criteria.LEFT_JOIN);

AFTER:

criteria.createAlias("habilidades", "h", JoinType.LEFT_OUTER_JOIN);

NOTE: no associationPath first parameter agent puts the attribute that is mapped in our base class.

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="PESSOAXHABILIDADE", joinColumns=
{@JoinColumn(name="PESSOAID")}, inverseJoinColumns=
{@JoinColumn(name="HABILIDADEID")})
private List<Habilidade> habilidades;

That's what I did and it worked!

    
10.02.2017 / 12:45