2017-05-08 155 views
1

我無法使用JpaRepository獲取嵌套對象的列表。我會盡力解釋我想用一下下面的代碼:如何獲得使用JpaRepository的嵌套對象列表?

汽車維修實體:

@Entity 
public class AutoService { 
    @Id 
    private long id; 

    @Column(name = "serviceName", nullable = false) 
    private String serviceName; 
} 

服務實體:

@Entity 
public class Service { 
    @Id 
    private long serviceId; 

    @Column(name = "serviceName", nullable = false) 
    private String serviceName; 

    @Column(name = "category", nullable = false) 
    private String category; 

    @ManyToOne 
    @JoinColumn(name = "autoServiceId", nullable = false) 
    private AutoService autoService; 
} 

ServiceRepository接口:

public interface ServiceRepository extends JpaRepository<Service, Long> { 
    List<Service> findByServiceNameAndCategory(String autoServiceName, String categoryName); 
} 

商業邏輯:

@org.springframework.stereotype.Service 
public class ServiceServiceImpl implements ServiceService { 
    @Autowired 
    private ServiceRepository serviceRepository; 

    @Override 
    public List<Service> findByAutoServiceAndCategory(String autoServiceName, String serviceCategory) { 
     return serviceRepository.findByServiceNameAndCategory(autoServiceName, serviceCategory); 
    } 
} 

正如我期待,上面的代碼是無法提供的Service小號匹配所提供的類別和AutoService名稱所需的列表。

有人可以提供建議,我應該如何使用我的資源庫獲取nester服務列表:autoServiceNameserviceCategory please?

編輯

現在我使用的是自定義查詢。

我現在正在使用autoServiceId而不是服務名稱。

但由於某種原因,我得到的對象的清單。

這是我的JPA回購。

public interface ServiceRepository extends JpaRepository<Service, Long> { 
    @Query("SELECT s from Service s where s.autoService.id = :autoServiceId and s.category = :categoryName") 
    List<Service> findByServiceNameAndCategory(@Param("autoServiceId") Long autoServiceId, @Param("categoryName") String categoryName); 
} 

有什麼建議嗎?

我想我知道答案。問題在我的類別中,發送到服務器。我用俄語寫了它。並在服務器端編碼類別的破碎值。 enter image description here

回答

2

1-使用@Embedded並相應@Embeddable註釋您的實體對象上,然後你的方法將獲取嵌套對象。

OR

2- @Query註釋用於編寫自定義查詢,請參閱以下鏈接custom query reference

1

您可能需要編寫一個查詢您的ServiceRepository

public interface ServiceRepository extends JpaRepository<Service, Long> { 

    @Query("SELECT s from Service s where s.autoService.serviceName = :autoServiceName and s.category = :categoryName") 
    Set<Round> getRoundsBySessionQuestionId(@Param("autoServiceName") String autoServiceName, @Param("categoryName") String categoryName); 

} 

希望這會有所幫助。快樂的編碼!

+0

不幸的是我現在得到空白列表。我現在使用autoServiceId而不是名稱。但出於某種原因,我從數據庫中獲取空數據列表。 – Andrew

+0

您是否能夠解決問題。上述查詢將從字面上解決它。如果有效,請將其標記爲正確的答案,因爲它可以幫助未來搜索相同的人。否則讓我知道當前的問題。對於我來說,如果你發送英文字符的類別它應該工作。 –

1

既然你有兩個AutoServiceService實體serviceName屬性,ServiceRepository.findByServiceNameAndCategory等同於以下SQL查詢:

SELECT 
    * 
FROM 
    Service 
WHERE 
    serviceName = ? 
AND category = ? 

正如所看到的,此查詢不打AutoService實體可言,這是爲什麼結果並不如預期。


正確的存儲庫的方法是:

public interface ServiceRepository extends JpaRepository<Service, Long> { 
    List<Service> findByCategoryAndAutoServiceServiceName(String category, String autoServiceName); 
} 

此方法將通過其serviceName查詢的嵌套AutoService對象,如所預期。

A sample project在Github上可用以顯示此功能。

+0

非常感謝您的回答,我現在正在使用自定義查詢,並從數據庫中獲取空列表。你能檢查我的編輯嗎? – Andrew

+0

@Andrew,你不需要自定義查詢,那就是我的答案。你可以簡單地使用'findByCategoryAndAutoServiceId'或類似的東西。如果這不適合你,請發佈我們可以測試的實際代碼和一些示例數據。 – manish

相關問題