2017-05-07 42 views
0

我有下一個架構: 2實體類,關係一對多。 一個汽車服務和它的許多服務。 現在我想通過名稱獲得一項汽車服務的所有服務。如何獲得服務器端的嵌套對象列表?一對多關係

我可以做到嗎?或者我應該發送客戶端完整的對象,並獲得列表後?

我有2個實體與所有的getters和setters。我不會發布它們來節省空間。

我AutoService.class

@Entity 
@Table(name = "AutoRate") 
public class AutoService { 

    public AutoService() { 
    } 

    @Id 
    @GeneratedValue(generator = "increment") 
    @GenericGenerator(name = "increment", strategy = "increment") 
    private long id; 

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

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

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

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

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

    @JsonManagedReference 
    @OneToMany(mappedBy = "autoService", fetch = FetchType.EAGER, cascade = CascadeType.PERSIST) 
    private List<Service> services = new ArrayList<Service>(); 
} 

Service.class

@Entity 
@Table(name = "Service") 
public class Service { 

    public Service() { 
    } 

    @Id 
    @GeneratedValue(generator = "increment") 
    @GenericGenerator(name = "increment", strategy = "increment") 
    @Column(name = "serviceId", unique = true, nullable = false) 
    private long serviceId; 

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

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

    @Column(name = "price", nullable = false) 
    private int price; 

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

我旁邊倉庫接口:

public interface AutoRateRepository extends JpaRepository<AutoService, Long> { 
    AutoService findByServiceName(String serviceName); 
    List<Service> findServicesByServiceName(String serviceName); 
} 

ServiceImpl

@Service 
public class AutoRateServiceImpl implements AutoRateService { 
@Override 
    public List<com.webserverconfig.user.entity.Service> getAllServicesByAutoServiceName(String name) { 
     return repository.findServicesByServiceName(name); 
    } 
} 
從我的控制器

和方法:

@RequestMapping(value = "/getServices/{serviceName}", method = RequestMethod.GET) 
    @ResponseBody 
    public List<Service> getServices(@PathVariable("serviceName") String serviceName){ 
     List<Service> services = dataBaseService.getAllServicesByAutoServiceName(serviceName); 
     return services; 
    } 

從數據庫結構: enter image description here

當我試圖獲得的服務列表當前出錯:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.webserverconfig.user.entity.AutoService] to type [com.webserverconfig.user.entity.Service]] with root cause 
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.webserverconfig.user.entity.AutoService] to type [com.webserverconfig.user.entity.Service] 

回答

0

我不會推薦混合生成不同實體的查詢(除JpaRepository<E,I>接口通用類型中指定的實體之外的其他實體)。

我會創造只爲Service實體查詢一個獨立的存儲設備,並將該方法裏邊有:

public interface ServiceRepository extends JpaRepository<Service, Long> { 
    List<Service> findByServiceName(String serviceName); 
} 

與您當前方法的問題是,春天總是會嘗試將結果轉換爲指定的類型在JpaRepository的通用參數中,即AutoService

+0

將來我會面臨一個問題,即框架無法通過autoservice名稱找到服務?如果我將把所有的服務都搬走? – Andrew

相關問題