【问题标题】:JPA : entities with null property not retrievedJPA:未检索到具有空属性的实体
【发布时间】:2015-03-02 14:06:44
【问题描述】:

我有两个实体:

@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 null 的客户端。

我该怎么做才能获得所有客户?

谢谢。

【问题讨论】:

    标签: jpa repository jpql


    【解决方案1】:

    这是因为您的查询在 ClientTypeClient 之间转换为 inner join。试试这个

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

    【讨论】:

    • 它不起作用,Tomcat 无法使用此查询启动服务器:原因:org.springframework.beans.factory.BeanCreationException:创建名为“clientRepository”的 bean 时出错:调用 init 方法失败的;嵌套异常是 java.lang.IllegalArgumentException: Validation failed for query for public abstract java.util.List com.myapp.repository.ClientRepository.findAllWithoutForeignKey()!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 2012-04-24
      相关资源
      最近更新 更多