I'm trying to do some joins between tables using criteria, but I'm getting the following error:
org.hibernate.query.SemanticException: Attribute [com.amfpromotora.entity.EmprestimosTabelas#tabcodigo(BASIC)] is not joinable
I know that to make the join work, I need to insert the @JoinColumn annotation in my entity, but in this case the foreign key is in the target tables.
Here is the relationship between the tables in my select
FROM emprestimos_tabelas t
JOIN emprestimos_bancos b ON t.banccodigo = b.banccodigo
JOIN emprestimos_comiss_agenciador c ON t.tabcodigo = c.tabcodigo
JOIN emprestimos_comiss_correspondente cc ON c.tabcodigo = cc.tabcodigo AND c.parmin = cc.parmin
Entities
@Entity
@Table(name = "emprestimos_tabelas", schema = "public", catalog = "joinconsig")
@Data
public class EmprestimosTabelas {
@Id
@Column(name = "tabcodigo")
private int tabcodigo;
@ManyToOne
@JoinColumn(name = "banccodigo")
private EmprestimosBancos banccodigo;
@Basic
@Column(name = "tabnome")
private String tabnome;
...
}
@Entity
@Table(name = "emprestimos_comiss_agenciador", schema = "public", catalog = "joinconsig")
@IdClass(EmprestimosComissAgenciadorPK.class)
@Data
public class EmprestimosComissAgenciador {
@Id
@ManyToOne
@JoinColumn(name = "tabcodigo")
private EmprestimosTabelas tabcodigo;
@Id
@Column(name = "parmin")
private int parmin;
...
}
@Entity
@Table(name = "emprestimos_comiss_correspondente", schema = "public", catalog = "joinconsig")
@IdClass(EmprestimosComissCorrespondentePK.class)
@Data
public class EmprestimosComissCorrespondente {
@Id
@Column(name = "tabcodigo")
private int tabcodigo;
@Id
@Column(name = "parmin")
private int parmin;
...
}
And here is my attempt at joins using CriteriaBuilder.
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<CommissionTable> query = cb.createQuery(CommissionTable.class);
Root<EmprestimosTabelas> root = query.from(EmprestimosTabelas.class);
List<Predicate> predicates = new ArrayList<>();
Join<EmprestimosTabelas, EmprestimosBancos> bancosJoin = root.join("banccodigo");
Join<EmprestimosComissAgenciador, EmprestimosTabelas> comissAgenciadorJoin = root.join("tabcodigo");
Join<EmprestimosComissAgenciador, EmprestimosComissCorrespondente> comissCorrespondenteJoin = comissAgenciadorJoin.join("tabcodigo");
I don't know what I'm doing wrong, if someone can help me, I appreciate it.