我無法使用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服務列表:autoServiceName
和serviceCategory
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);
}
有什麼建議嗎?
我想我知道答案。問題在我的類別中,發送到服務器。我用俄語寫了它。並在服務器端編碼類別的破碎值。
不幸的是我現在得到空白列表。我現在使用autoServiceId而不是名稱。但出於某種原因,我從數據庫中獲取空數據列表。 – Andrew
您是否能夠解決問題。上述查詢將從字面上解決它。如果有效,請將其標記爲正確的答案,因爲它可以幫助未來搜索相同的人。否則讓我知道當前的問題。對於我來說,如果你發送英文字符的類別它應該工作。 –