I'm having a relatively simple problem to solve, but I'm not getting a logical disability. My problem is this:
I have a hierarchy of leaders and leaders. For example:
Lider 1
Lider 1.1
Lider 1.1.1
Lider 1.1.2
Lider 1.1.3
Lider 1.1.3.1
Lider 1.2
Lider 1.2.1
Lider 1.3
And so it goes. How many levels are needed. In the database, this is already mapped. That is, I can tell who is leading who using the following sample query:
select * from colaboradores where lider_id = ?;
However, I am not able to build an efficient Java recursion to return all the leaders below. For example, if I pass to my method the ID of the leader 1.1, should it return to me a list like this:
Lider 1.1
Lider 1.1.1
Lider 1.1.2
Lider 1.1.3
Lider 1.1.3.1
Does anyone have a light for me there?
UPDATE: I tried to map JPA itself. Just a note: The relationship between leaders and leaders is not done via the class ID, but rather another field that is not a foreign key. This other field is tuition. See:
@ManyToOne(targetEntity = Colaborador.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "matricula_lider", referencedColumnName = "matricula", insertable = false, updatable = false)
private Colaborador lider;
@OneToMany(mappedBy="lider", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private List<Colaborador> liderados;
Well, if I use only the mapping:
@ManyToOne(targetEntity = Colaborador.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "matricula_lider", referencedColumnName = "matricula", insertable = false, updatable = false)
private Colaboradorlider;
It works cool and I get the leader of the person. If I use both mappings (bi-directional), the error when trying to recover. In fact, it does not give an error, it seems that it goes into an infinite loop until the timeout of the transaction.
javax.persistence.PersistenceException: org.hibernate.HibernateException: Transaction was rolled back in a different thread!
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
...
Is my mapping wrong?