user1260928
user1260928

Reputation: 3429

JPA : entities with null property not retrieved

I have two entities :

@Entity
@Table(name = "T_CLIENT")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Client implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "code")
    private String code;

    @Column(name = "nom")
    private String nom;

    @OneToOne
    private TypeClient typeClient;



@Entity
@Table(name = "T_TYPECLIENT")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class TypeClient implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "nom")
    private String nom;

I want to do that query :

@Query("Select id, nom, code, codeComptable, typeClient from Client")
List<Object[]>  findAllWithoutForeignKey();

JPA returns only client with typeClient <> null.

How do I have to do to get all clients?

Thanks.

Upvotes: 1

Views: 89

Answers (2)

V G
V G

Reputation: 19002

The relationship Client to TypeClient is not OneToOne, but rather ManyToOne. Try changing that, and it might work.

Upvotes: 0

Predrag Maric
Predrag Maric

Reputation: 24433

That's because your query translates to inner join between Client and TypeClient. Try this

@Query("Select c.id, c.nom, c.code, c.codeComptable, tc from Client c left join c.typeClient tc")

Upvotes: 1

Related Questions