0

Ok guys, this is driving me crazy. So...

This is my Empresa class relationship:

@ManyToOne
@JoinColumn(name = "consultor_id", nullable = false)
private Consultor consultor;

There are some other mappings like:

@OneToMany(mappedBy = "empresa", cascade = CascadeType.ALL)
private Set<MelhoriasAtingidas> ma = new LinkedHashSet<>();

@OneToMany(mappedBy = "empresa", cascade = CascadeType.ALL)
private Set<Atividades> at = new LinkedHashSet<>();

@OneToMany(mappedBy = "empresa", cascade = CascadeType.ALL)
private Set<Pessoa> set = new LinkedHashSet<>();

And this is my consultor class,the id, and the mappings:

@Id
@Column(name = "id", nullable = false)
@GeneratedValue
private int id;

@OneToMany(mappedBy = "consultor", cascade = CascadeType.ALL)
private Set<MelhoriasAtingidas> ma = new LinkedHashSet<MelhoriasAtingidas>();

@OneToMany(mappedBy = "consultor", cascade = CascadeType.ALL)
private Set<Atividades> at = new LinkedHashSet<Atividades>();

@OneToMany(mappedBy = "consultor", cascade = CascadeType.ALL)
private Set<Empresa> em = new LinkedHashSet<Empresa>();

I'm trying to query all the Empresas that belongs to a given Consultor, using Hibernate's TypedQuery:

public List<Empresa> getEmpresas(Integer id){
. standard entity manager stuff here
.
.
TypedQuery<Empresa> query = manager.createQuery("from Empresa e where 
e.consultor.id = ?", Empresa.class);
query.setParameter(1,id);
...
}

When i do that: "from Empresa e where e.consultor.id = ?", Empresa.class, i get a Stack Overflow Error.

This is the part of Hibernate's log that i want to do:

2017-06-28T18:43:19.842-0300|Informações: Hibernate: 
      select
        empresa0_.id as id1_2_,
        empresa0_.cnpj as cnpj2_2_,
        empresa0_.consultor_id as consulto5_2_,
        empresa0_.criado as criado3_2_,
        empresa0_.nome as nome4_2_ 
    from
        empresa empresa0_ 
    where
        empresa0_.consultor_id=?
2017-06-28T18:43:19.848-0300|Informações: Hibernate: 
    select
        consultor0_.id as id1_1_0_,
        consultor0_.criado as criado2_1_0_,
        consultor0_.nome as nome3_1_0_ 
    from
        consultor consultor0_ 
    where
        consultor0_.id=?

And then Hibernate do thousands of this:

2017-06-28T18:43:19.860-0300|Informações: Hibernate: 
    select
        at0_.empresa_id as empresa13_2_0_,
        at0_.id as id1_0_0_,
        at0_.id as id1_0_1_,
        at0_.area as area2_0_1_,
        at0_.atividades as atividad3_0_1_,
        at0_.consultor_id as consult12_0_1_,
        at0_.criado as criado4_0_1_,
        at0_.data as data5_0_1_,
        at0_.empresa_id as empresa13_0_1_,
        at0_.ferramenta as ferramen6_0_1_,
        at0_.fim as fim7_0_1_,
        at0_.inicio as inicio8_0_1_,
        at0_.programa as programa9_0_1_,
        at0_.status_id as status_14_0_1_,
        at0_.tempo as tempo10_0_1_,
        at0_.tipo as tipo11_0_1_,
        consultor1_.id as id1_1_2_,
        consultor1_.criado as criado2_1_2_,
        consultor1_.nome as nome3_1_2_,
        status2_.id as id1_8_3_,
        status2_.criado as criado2_8_3_,
        status2_.status as status3_8_3_ 
    from
        atividades at0_ 
    inner join
        consultor consultor1_ 
            on at0_.consultor_id=consultor1_.id 
    inner join
        status status2_ 
            on at0_.status_id=status2_.id 
    where
        at0_.empresa_id=?

What's happening? Am I terrible?

First question btw, so be nice :)

Joe Taras
  • 15,166
  • 7
  • 42
  • 55
  • Solved by removing hashCode function from models...But i don't know the consequences... – Giovanni Klein Campigoto Jun 29 '17 at 01:50
  • Depending on how `hashCode` and `equals` is implemented, it may trigger lazy loading on to-many associations. Try to generate your `hashCode` and `equals` so that they skip relationships between entities. For a discussion of possible approaches to implementing `equals/hashCode`, see: https://stackoverflow.com/questions/2446590/entities-equals-hashcode-and-tostring-how-to-correctly-implement-them – crizzis Jun 29 '17 at 08:24

0 Answers0