2015-03-02 35 views
1

實體我有兩個實體:JPA:與空屬性沒有檢索

@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 <>空只返回客戶端。

如何獲得所有客戶?

謝謝。

回答

1

這是因爲您的查詢翻譯爲inner join之間ClientTypeClient。試試這個

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

它不起作用,Tomcat無法啓動該查詢服務器:引起:org.springframework.beans.factory.BeanCreationException:創建名爲'clientRepository'的bean時出錯:init方法的調用失敗;嵌套異常是java.lang.IllegalArgumentException:對於方法的查詢驗證失敗public abstract java.util.List com.myapp.repository.ClientRepository.findAllWithoutForeignKey()! – user1260928 2015-03-02 14:15:07

+0

對不起,我編輯了我的答案。現在試試。 – 2015-03-02 14:29:37

0

的關係Client到TypeClient不OneToOne,而是ManyToOne。嘗試改變它,它可能工作。