JPA:未检索到null属性的实体

时间:2015-03-02 14:06:44

标签: jpa repository jpql

我有两个实体:

@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;

我想做那个查询:

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

JPA仅返回typeClient&lt;&gt;的客户端空。

如何获得所有客户?

感谢。

2 个答案:

答案 0 :(得分:1)

这是因为您的查询会在inner joinClient之间转换为TypeClient。试试这个

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

答案 1 :(得分:0)

与TypeClient的Client关系不是OneToOne,而是ManyToOne。尝试改变它,它可能会起作用。