2
I have five entities (Quotation, Sector, Partner, Items and Items), where the user will select a quotation and sector and the list of all items related to this sector will appear, along with the items that have already been launched (if they have).
I used the following select to bring the records:
String jpql = "SELECT i "
+ "FROM Itens i "
+ "INNER JOIN i.setor s "
+ "LEFT JOIN i.itensLancados il "
+ "ON il.parceiro.codParceiro = 50 AND il.cotacao.codCotacao = 1 "
+ "WHERE s.codSetor = 1";
But because the entity Itensreleased as EAGER, is bringing me all the records of this table, ignoring the restriction of partner code and quotation code.
I tried to fetch after the LEFT JOIN, but then I can’t use the ON clause, and if I try to play the restriction of the ON class to the WHERE it doesn’t work, it will filter only the data of the table Itenslancados, not bringing all the items.
Would anyone have any tips on how to solve this case?
Relationships
Quotation
@OneToMany(mappedBy = "cotacao", fetch = FetchType.LAZY)
private List<ItensLancados> itensLancados;
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name = "cotacao_parceiro", joinColumns = @JoinColumn(name = "cotacao_id"), inverseJoinColumns = @JoinColumn(name = "parceiro_id"))
private List<Parceiro> parceiros;
@ManyToMany(fetch=FetchType.LAZY)
@JoinTable(name = "cotacao_setor", joinColumns = @JoinColumn(name = "cotacao_id"), inverseJoinColumns = @JoinColumn(name = "setor_id"))
private List<Setor> setores;
Sector
@OneToMany(mappedBy = "setor", fetch = FetchType.LAZY)
private List<Itens> itens;
@ManyToMany(mappedBy = "setores", fetch = FetchType.LAZY)
private List<Cotacao> cotacoes;
Partner
@OneToMany(mappedBy = "parceiro", fetch = FetchType.LAZY)
private List<ItensLancados> itensLancados;
@ManyToMany(mappedBy = "parceiros", fetch = FetchType.LAZY)
private List<Cotacao> cotacoes;
Items
@OneToMany(mappedBy = "itens", fetch = FetchType.LAZY)
private List<ItensLancados> itensLancados;
@ManyToOne
private Setor setor;
Points
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cotacao_id")
private Cotacao cotacao;
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "item_id")
private Itens itens;
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parceiro_id")
private Parceiro parceiro;
The Select you would like to convert to JPQL is as follows:
SELECT *
FROM ITENSLANCADOS RIGHT JOIN ITENS
ON ITENSLANCADOS.ITEM_ID = ITENS.IDITENS
AND itenslancados.cotacao_id = 50
and itenslancados.parceiro_id = 1
WHERE setor_codsetor = 1
Follow the SQL generated by JPA.
select
itens0_.idItens as idItens1_1_0_,
itenslanca2_.cotacao_id as cotacao_1_2_1_,
itenslanca2_.item_id as item_id2_2_1_,
itenslanca2_.parceiro_id as parceiro3_2_1_,
itens0_.codigoPlu as codigoPl2_1_0_,
itens0_.descricaoItem as descrica3_1_0_,
itens0_.eanItem as eanItem4_1_0_,
itens0_.embalagem as embalage5_1_0_,
itens0_.setor_codSetor as setor_co7_1_0_,
itens0_.statusItem as statusIt6_1_0_,
itenslanca2_.dataLancamento as dataLanc4_2_1_,
itenslanca2_.qtdDigitada as qtdDigit5_2_1_,
itenslanca2_.valorDigitado as valorDig6_2_1_,
itenslanca2_.item_id as item_id2_1_0__,
itenslanca2_.cotacao_id as cotacao_1_2_0__,
itenslanca2_.item_id as item_id2_2_0__,
itenslanca2_.parceiro_id as parceiro3_2_0__
from
Itens itens0_
inner join
Setor setor1_
on itens0_.setor_codSetor=setor1_.codSetor
left outer join
ItensLancados itenslanca2_
on itens0_.idItens=itenslanca2_.item_id
left outer join
ItensLancados itenslanca3_
on itens0_.idItens=itenslanca3_.item_id
inner join
Parceiro parceiro4_
on itenslanca3_.parceiro_id=parceiro4_.codParceiro
and (
parceiro4_.codParceiro=1
)
inner join
Cotacao cotacao5_
on itenslanca3_.cotacao_id=cotacao5_.codCotacao
and (
cotacao5_.codCotacao=50
)
where
setor1_.codSetor=1
First of all... remove this
EAGER
is an option? In general, useEAGER
brings much more problems than solutions.– Dherik
You cannot make the query return launched items instead of Item?
– Dherik
Sorry, I didn’t see that. So, but reversing and placing the WHERE with the partner conditions and quotation will limit the result to only the items released and will not bring the items that were not released even using the RIGHT JOIN. I tried to do in BD the query this way (reversing the logic) and also did not roll.
– Ismael Junior
Do you have the SQL query you’d like to do with JPQL? If so, put it on http://sqlfiddle.com/. I’m not sure what you’d like to know.
– Dherik
Check to see if you can access.. http://sqlfiddle.com/#! 15/84e71
– Ismael Junior
Cool, but the SQL that you would like to become a JPQL, you can put there too?
– Dherik
I updated it there.. Please check if it appears please
– Ismael Junior
SQL does not appear. Link is correct?
– Dherik
So, it is .. but I don’t know why SQL didn’t appear.. I updated there in the statement with SQL that I would like to use.
– Ismael Junior
I want to see the result of the query, but SQL Fiddle is having difficulties... I will try again later.
– Dherik