Problem in left Join JPA - JPQL

Asked

Viewed 237 times

2

Exception in thread "main" javax.persistence.Entitynotfoundexception: Unable to find CNPJ with id 00001388000307

Reading the documentation, I saw that this Exception is released when it tries to access (by the method getReference of the interface EntityManager, which has the LAZY FETCH behavior) and the entity does not exist.

Thrown by the persistence Provider when an Entity Reference obtained by Entitymanager.getReference is accessed but the Entity does not exist.

I have the following entities: Vendedor and CNPJ, where there may be many sellers with the same CNPJ, ie a relationship @ManyToOne.

This relationship is working OK, the way it’s mapped out. 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 anome relationship (THAT EVERYTHING IS OK) and the relationship with the CNPJ, is returning to Exception, when having a LEFT JOIN ,which I highlighted at the beginning.

Not using LEFT JOIN, a RIGHT JOIN or INNER JOIN, works normally, comes all Sellers with CNPJ and no Exceptions released, MAAAS, as some sellers do not have CNPJ and need to bring them too, there is the 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;
    }
}
  • 1

    Will @NotFound(action = NotFoundAction.IGNORE) doesn’t solve?

  • @But it didn’t solve ;(

  • 1

    Try to remove the fetch and see if the error continues.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.