2017-07-24 41 views
0

我有一個包含多個表的實體,與一對多關聯:JPA CrudRepository在搜索列表中選擇

@Entity 
public class Data { 

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) 
    private List<Tool> toolList= new ArrayList<Tool>(); 

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) 
    private List<Level> levelList = new ArrayList<Level>(); 
} 

我想編寫一個@Query在CrudRepository選擇包含在列表中的數據項toolList工具的子集(List子集)。我很確定這是否可行。

+0

你的控制器你想創建一個自定義回購?並將其附加到您的粗俗存儲庫? –

+0

不,在我的CrudRepository中,我想添加一個自定義查詢的方法。但是從我的toolList我想檢查一個subsetToolList中的元素是否在toolLits中,然後我返回數據。 –

回答

0

接口

public interface Repository extends CrudRepository<Data, Integer //or your primary key>{ 

     @Query(value = "write the jpql query") 
     eg - @Query(value = "select s.studentname from StudentEntity s where s.age > 20") 
     List<String> query(); 

} 

在服務

@Transactional 
public class Service { 

    private final Repository repository; 

    public Service(Repository repository){ 
     this.repository = repository; 
    } 


    public List<String> query(){ 
     return repository.query();  
    } 

} 

現在使用的服務使用自動裝配Autowired

+0

我不確定查詢是否可能,這是我的問題。 –

+0

我已經使用它請chexk這個回購https://github.com/rahulrsingh09/Spring-Boot-DataJPA –