Skip to content

Instantly share code, notes, and snippets.

@rponte
Last active January 29, 2021 19:17
Show Gist options
  • Save rponte/7911304 to your computer and use it in GitHub Desktop.
Save rponte/7911304 to your computer and use it in GitHub Desktop.
Example of HQL Left Join
@Entity
public class Bem implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(nullable = false)
private Lotacao lotacao;
@Transient
private boolean estaAtreladoAMovimentacao;
/**
* Default constructor
* /
public Bem() {}
public Bem(Bem bem, boolean estaAtreladoAMovimentacao) {
this.id = bem.id;
this.lotacao = bem.lotacao;
this.estaAtreladoAMovimentacao = estaAtreladoAMovimentacao;
}
public Bem(Bem bem, Long movId) {
this.id = bem.id;
this.lotacao = bem.lotacao;
this.estaAtreladoAMovimentacao = movId != null;
}
}
select new Bem(bem, case when mov.id is not null then true else false end)
from Movimentacao mov
left join mov.bem as bem
with bem.lotacao = :lotacao
where mov.id = 69
select new Bem(bem, mov.id)
from Movimentacao mov
left join mov.bem as bem
with bem.lotacao = :lotacao
where mov.id = 69
@Entity
public class Movimentacao implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(nullable = false)
private Bem bem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment