Exception in thread "main" javax.persistence.EntityNotFoundException: Unable to find CNPJ with id 00001388000307
Reading the documentation, I saw that this exception is thrown when it tries to access (by the getReference
method of the EntityManager
interface, which has the FETCH LAZY behavior) and the entity does not exist.
I have the following entities: Vendedor
and CNPJ
, where there may be many Sellers with the same CNPJ, that is a @ManyToOne
relation.
This relationship is working OK, the way it is mapped. It turns out that when I try to execute the following query
select r from Vendedor r join fetch r.anoMes left join fetch r.cnpj
To bring the sellers with their relationship of years (THAT IS ALL OK) and the relationship with the CNPJ, is returning to Exception, when having to do a LEFT JOIN, which I highlighted at the beginning.
Not using LEFT JOIN, a RIGHT JOIN OR INNER JOIN, works normally, comes all the Sellers with CNPJ and nothing Exceptions released, MAAAS, as some sellers do not have CNPJ and need to bring them also, there is a need to do a LEFT JOIN
@Entity
public class Vendedor{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@JoinColumn(name = "ano_mes")
@ManyToOne
private AnoMes anoMes;
@JoinColumn(name = "cnpj")
@ManyToOne
private CNPJ cnpj;
public AnoMes getAnoMes() {
return anoMes;
}
public CNPJ getCnpj() {
return cnpj;
}
}
@Entity
public class CNPJ {
@Id
@Column(name = "CCG", length = 14)
private String ccg;
public String getCcg() {
return ccg;
}
}