Spring PagingAndSortingRepository - 按名称查找 [英] Spring PagingAndSortingRepository - find by name
问题描述
问题:我试图在网格中只加载国名为购买订单...让我们说爱尔兰。事情是我真的不知道这是否可能。
到目前为止,我已经尝试将服务类 executeQueryFindAll()
中的方法更改为这样的内容。
私人网页< PurchaseOrder> executeQueryFindAll(int page,int maxResults){
final PageRequest pageRequest = new PageRequest(page,maxResults,sortByNameASC());
页面< PurchaseOrder> purchaseOrders = purchaseOrderRepository.findAll(pageRequest);
页< PurchaseOrder> selectedPurchaseOrders =新页面< PurchaseOrder>(); //不能实例化页面是抽象
(PurchaseOrder next:purchaseOrders){
if(next.getCountry()。getCountryname()。toString()。equals(Ireland))
selectedPurchaseOrders.add(next); //这是跛脚,因为不是LIST,所以它不会工作
}
return selectedPurchaseOrders;
上面的方法不会工作,因为Page是一个抽象类,因此不能被实例化。我可以循环但不能执行 selectedPurchaseOrders.add(next)
,因为那不是List。在调试模式下,我可以评估 next.getCountry()。getCountryname()。toString()
,我得到国家名称,这就是为什么我试图比较字符串对象 .equals(Ireland))
。
有任何建议吗?
Model
@Entity
@Table(name = PURCHASEORDER,schema =POTOOL)
公共类PurchaseOrder {
@Id
@GeneratedValue
private int id;
//其他PurchaseOrder属性
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name =country_id)
私人国家国家;
// Getter和Setters
存储库
public interface PurchaseOrderRepository扩展
PagingAndSortingRepository< PurchaseOrder,Integer> {
页< PurchaseOrder> findByTowerLike(可分页,字符串名称);
}
服务
@Service
@Transactional
公共类PurchaseOrderService {
$ b @Autowired
私有PurchaseOrderRepository purchaseOrderRepository;
@Transactional(readOnly = true)
public PurchaseOrderListVO findAll(int page,int maxResults){
Page< PurchaseOrder> result = executeQueryFindAll(page,maxResults);
if(shouldExecuteSameQueryInLastPage(page,result)){
int lastPage = result.getTotalPages() - 1;
result = executeQueryFindAll(lastPage,maxResults);
}
return buildResult(result);
}
私人网页< PurchaseOrder> executeQueryFindAll(int page,int maxResults){
final PageRequest pageRequest = new PageRequest(page,maxResults,sortByNameASC());
返回purchaseOrderRepository.findAll(pageRequest);
//其余的类
无论如何,因为这看起来像像一个Spring Data项目,你应该真的阅读文档来看看有什么可能的:
根据是否需要分页,需要在存储库一侧添加一个方法到你的 PurchaseOrderRepository
接口:
页面<&PurchaseOrder的GT; findByCountryCountryName(可分页,字符串名称);
列表< PurchaseOrder> findByCountryCountryName(String name);
没有必要创建一个实现,因为Spring Data会这样做,并创建一个基于方法名称。
存储库代理有两种方法从方法名称派生特定于存储的查询
。 它可以直接从方法名称
派生查询,或者使用手动定义的查询。
您只需从服务层调用您的新方法即可:
@Service
@Transactional
公共类PurchaseOrderService {
@Autowired
私人PurchaseOrderRepository purchaseOrderRepository;
....
私人清单< PurchaseOrder> loadByCountry(String country){
return purchaseOrderRepository.findByCountryCountryName(country);
}
}
Problem: I am trying to load into the grid only the Purchase Orders for which country name is...let's say Ireland. Thing is that I don't really know if that's possible.
So far I've tried to change the method in the service class executeQueryFindAll()
to something like this.
private Page<PurchaseOrder> executeQueryFindAll(int page, int maxResults) {
final PageRequest pageRequest = new PageRequest(page, maxResults, sortByNameASC());
Page<PurchaseOrder> purchaseOrders = purchaseOrderRepository.findAll(pageRequest);
Page<PurchaseOrder> selectedPurchaseOrders = new Page<PurchaseOrder>(); //not possibile to instantiate as Page is abstract
for(PurchaseOrder next : purchaseOrders){
if(next.getCountry().getCountryname().toString().equals("Ireland"))
selectedPurchaseOrders.add(next); //thats lame because is not a LIST so it wont work
}
return selectedPurchaseOrders;
}
The above approach wont work because Page is an abstract class, therefore cannot be instantiated. I can loop through but I can't do selectedPurchaseOrders.add(next)
because that's not a List. In debug mode I can evaluate the next.getCountry().getCountryname().toString()
and I get the country name and that's why I've tried to compare the String object .equals("Ireland"))
.
Any suggestion please?
Model
@Entity
@Table(name = "PURCHASEORDER",schema = "POTOOL")
public class PurchaseOrder {
@Id
@GeneratedValue
private int id;
//other PurchaseOrder properties
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "country_id")
private Country country;
//Getter and Setters
Repository
public interface PurchaseOrderRepository extends
PagingAndSortingRepository<PurchaseOrder, Integer> {
Page<PurchaseOrder> findByTowerLike(Pageable pageable, String name);
}
Service
@Service
@Transactional
public class PurchaseOrderService {
@Autowired
private PurchaseOrderRepository purchaseOrderRepository;
@Transactional(readOnly = true)
public PurchaseOrderListVO findAll(int page, int maxResults) {
Page<PurchaseOrder> result = executeQueryFindAll(page, maxResults);
if(shouldExecuteSameQueryInLastPage(page, result)){
int lastPage = result.getTotalPages() - 1;
result = executeQueryFindAll(lastPage, maxResults);
}
return buildResult(result);
}
private Page<PurchaseOrder> executeQueryFindAll(int page, int maxResults) {
final PageRequest pageRequest = new PageRequest(page, maxResults, sortByNameASC());
return purchaseOrderRepository.findAll(pageRequest);
}
//rest of the class
解决方案 Why would you want to load all records and filter in memory rather than in the database?
Anyway, as this looks like a Spring Data project you should really read the documentation to see what is possible:
Depending on whether or not you require paging, all you need to do on the repository side is add a method to your PurchaseOrderRepository
interface:
Page<PurchaseOrder> findByCountryCountryName(Pageable pageable, String name);
List<PurchaseOrder> findByCountryCountryName(String name);
There is no need to create an implementation as Spring Data will do this and create a query based on the method name.
The repository proxy has two ways to derive a store-specific query
from the method name. It can derive the query from the method name
directly, or by using a manually defined query.
You simply then need to call your new method from your service layer:
@Service
@Transactional
public class PurchaseOrderService {
@Autowired
private PurchaseOrderRepository purchaseOrderRepository;
....
private List<PurchaseOrder> loadByCountry(String country) {
return purchaseOrderRepository.findByCountryCountryName(country);
}
}
这篇关于Spring PagingAndSortingRepository - 按名称查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文