-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Problem with hibernate + restrictions + OneToMany
PostPosted: Wed Mar 03, 2010 5:52 pm 
Newbie

Joined: Thu Feb 04, 2010 10:10 am
Posts: 8
Hi,

I have the following releationship:

Code:
Departamento.java

@OneToMany(targetEntity=Cargo.class, mappedBy="departamento", fetch=FetchType.EAGER)
@JoinColumn(name="fk_depId")
private List<Cargo> cargos;


Code:
Cargo.java

@ManyToOne(targetEntity=Departamento.class)
@JoinColumn(name="fk_depId")
@ForeignKey(name="dep_id")
private Departamento departamento;

@ManyToOne(targetEntity=Empresa.class)
@JoinColumn(name="fk_empId")
@ForeignKey(name="emp_id")
private Empresa empresa;


What i want to do is: a query in all Departamento where Empresa of the Cargo is DIFFERENT from "terceiro". I'm trying this:

Code:
Criteria crit = getSession().createCriteria(getPersistentClass());
crit.createAlias("cargos", "c");
crit.createAlias("c.empresa", "e");
crit.add(Restrictions.ne("e.apelido", "terceiro")).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);;
return (List<Departamento>) crit.list();


But it doesn't work. What i'm doing wrong?

Regards,
André Vendramini


Top
 Profile  
 
 Post subject: Re: Problem with hibernate + restrictions + OneToMany
PostPosted: Thu Mar 04, 2010 10:14 am 
Newbie

Joined: Thu Feb 04, 2010 10:10 am
Posts: 8
Hi,

I tested another queries, like filter on alias "c" (ex. Restrictions.eq("c.id", 5L)) and it works. The problem is when I try to filter by Empresa of Cargo. I'm using FetchType.EAGER.

Regards,
André Vendramini


Top
 Profile  
 
 Post subject: Re: Problem with hibernate + restrictions + OneToMany
PostPosted: Fri Mar 05, 2010 5:10 pm 
Newbie

Joined: Thu Feb 04, 2010 10:10 am
Posts: 8
Friends, someone could help me?

Regards,
André Vendramini


Top
 Profile  
 
 Post subject: Re: Problem with hibernate + restrictions + OneToMany
PostPosted: Tue Mar 09, 2010 12:15 am 
Newbie

Joined: Thu Feb 04, 2010 10:10 am
Posts: 8
Sorry for the up post, but nobody here could solve this issue?
I'm a front-end developer (actionscript) and my little experience with database is to build the queries on hands. This project needs to be developed on hibernate, so it's my first time with it.

Regards,
André Vendramini


Top
 Profile  
 
 Post subject: Re: Problem with hibernate + restrictions + OneToMany
PostPosted: Tue Mar 09, 2010 6:20 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
What is not working? What's the complete error message, failure trace?


Top
 Profile  
 
 Post subject: Re: Problem with hibernate + restrictions + OneToMany
PostPosted: Wed Mar 10, 2010 12:21 am 
Newbie

Joined: Thu Feb 04, 2010 10:10 am
Posts: 8
I don't have any errors. This is my test:

Code:
public class DepartamentoTest extends TestCase
{
   public void testDepartamentoSemTerceiro()
   {
      List<Departamento> dptos = new Departamento().listarPorAgencia();
      for (Departamento departamento : dptos)
      {
         List<Recurso> cargos = departamento.getCargos();
         for (Recurso recurso : cargos)
         {
            System.out.println(departamento.getId() + " " + departamento.getNome() + " >>>> " + recurso.getNome() + " >>>> " + recurso.getEmpresa().getApelido() + " " + recurso.getId());
         }
      }
   }
}


My method "listarPorAgencia()":

Code:
public List<Departamento> listarPorAgencia()
      {
         Criteria crit = getSession().createCriteria(getPersistentClass()).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
         crit.createAlias("cargos", "c");
         crit.createAlias("c.empresa", "e");
         crit.add(Restrictions.ne("e.apelido", "terceiro")).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
         return (List<Departamento>) crit.list();
      }


And this is my trace:

Quote:
1 CRIAÇAO >>>> Diretor de Arte >>>> Tyujk 2
1 CRIAÇAO >>>> Redator >>>> Tyujk 7
1 CRIAÇAO >>>> Redator >>>> Finga 8
1 CRIAÇAO >>>> Diretor de Arte >>>> terceiro 10
1 CRIAÇAO >>>> Redator >>>> terceiro 15
2 TECNOLOGIA >>>> Programador ActionScript >>>> Finga 1
2 TECNOLOGIA >>>> Programador PHP >>>> Finga 6
2 TECNOLOGIA >>>> Programador ActionScript >>>> terceiro 9
2 TECNOLOGIA >>>> Programador PHP >>>> terceiro 14
3 PLANEJAMENTO >>>> Planejador >>>> Finga 5
3 PLANEJAMENTO >>>> Planejador >>>> terceiro 13
4 PROJETOS >>>> Gerente de Projetos >>>> Finga 3
4 PROJETOS >>>> Gerente de Projetos >>>> terceiro 11
5 ATENDIMENTO >>>> Atendimento >>>> Finga 4
5 ATENDIMENTO >>>> Atendimento >>>> terceiro 12


See that it brings me terceiro? But I don't want this type of Empresa, just the others (Tyujk and Finga).

Thanks,
André Vendramini


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.