JPA:未检索到具有 null 属性 的实体
JPA : entities with null property not retrieved
我有两个实体:
@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 returns 只有 typeClient <> null 的客户端。
我要怎么做才能获得所有客户?
谢谢。
那是因为您的查询在 Client
和 TypeClient
之间转换为 inner join
。试试这个
@Query("Select c.id, c.nom, c.code, c.codeComptable, tc from Client c left join c.typeClient tc")
与 TypeClient 的关系 Client
不是 OneToOne
,而是 ManyToOne
。尝试更改它,它可能会起作用。
我有两个实体:
@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 returns 只有 typeClient <> null 的客户端。
我要怎么做才能获得所有客户?
谢谢。
那是因为您的查询在 Client
和 TypeClient
之间转换为 inner join
。试试这个
@Query("Select c.id, c.nom, c.code, c.codeComptable, tc from Client c left join c.typeClient tc")
与 TypeClient 的关系 Client
不是 OneToOne
,而是 ManyToOne
。尝试更改它,它可能会起作用。