Ari*_*aul 4 java hibernate jpql spring-boot
我是 Spring Boot 和 Hibernate 的新手。在这里,我尝试运行基于搜索的可选参数查询,我可以按名称、国家/地区等进行搜索。如果我将此字段保留为空,则查询应该全部列出。但问题是我的方法返回所有数据,忽略我的搜索参数。我的模型类看起来像
@Entity(name="MLFM_ORDER_OWNER")
public class ModelOrderOwner {
@Id @GenericGenerator(name = "custom_sequence", strategy =
"com.biziitech.mlfm.IdGenerator")
@GeneratedValue(generator = "custom_sequence")
@Column(name="ORDER_OWNER_ID")
private Long orderOwnerId;
@Column(name="OWNER_NAME")
private String ownerName;
@OneToOne
@JoinColumn(name="BUSINESS_TYPE_ID")
private ModelBusinessType businessTypeId;
@Column(name="SHORT_CODE")
private String shortCode;
@ManyToOne
@JoinColumn(name="OWNER_COUNTRY")
private ModelCountry ownerCountry;
// getter setter..
Run Code Online (Sandbox Code Playgroud)
我的存储库界面看起来像
public interface OrderOwnerRepository extends
JpaRepository<ModelOrderOwner,Long>{
@Query("select a from MLFM_ORDER_OWNER a where a.businessTypeId.typeId=coalsec(:typeId,a.businessTypeId.typeId) and a.ownerCountry.countryId=coalsec(:countryId,a.ownerCountry.countryId) and a.ownerName LIKE %:name and a.shortCode LIKE %:code")
public List <ModelOrderOwner> findOwnerDetails(@Param("typeId")Long typeId,@Param("countryId")Long countryId,@Param("name")String name,@Param("code")String code);
}
Run Code Online (Sandbox Code Playgroud)
这是我在控制器中的方法
@RequestMapping(path="/owners/search")
public String getAllOwner(Model model,@RequestParam("owner_name") String name,@RequestParam("shortCode") String code,
@RequestParam("phoneNumber") String phoneNumber,@RequestParam("countryName") Long countryId,
@RequestParam("businessType") Long typeId
) {
model.addAttribute("ownerList",ownerRepository.findOwnerDetails(typeId, countryId, name, code));
return "data_list";
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以在这方面帮助我吗?请?
现在回答也太晚了,但是对于任何寻找解决方案的人来说,还有一个更简单的方法,如下所示:
就我而言,我的控制器就像:
@RestController
@RequestMapping("/order")
public class OrderController {
private final IOrderService service;
public OrderController(IOrderService service) {
this.service = service;
}
@RequestMapping(value = "/{username}/", method = RequestMethod.GET)
public ResponseEntity<ListResponse<UserOrdersResponse>> getUserOrders(
@RequestHeader Map<String, String> requestHeaders,
@RequestParam(required=false) Long id,
@RequestParam(required=false) Long flags,
@RequestParam(required=true) Long offset,
@RequestParam(required=true) Long length) {
// Return successful response
return new ResponseEntity<>(service.getUserOrders(requestDTO), HttpStatus.OK);
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我有Usernameas@PathVariable和length,offset哪些是我必需的参数,但我接受id和flags用于过滤搜索结果,因此它们是我的可选参数,对于调用 REST 服务不是必需的。
现在,在我的存储库层中,我刚刚创建了@Query如下内容:
@Query("select new com.ada.bourse.wealth.services.models.response.UserOrdersResponse(FIELDS ARE DELETED TO BECOME MORE READABLE)" +
" from User u join Orders o on u.id = o.user.id where u.userName = :username" +
" and (:orderId is null or o.id = :orderId) and (:flag is null or o.flags = :flag)")
Page<UserOrdersResponse> findUsersOrders(String username, Long orderId, Long flag, Pageable page);
Run Code Online (Sandbox Code Playgroud)
就是这样,您可以看到我用(:orderId is null or o.id = :orderId)and检查了我的可选参数(:flag is null or o.flags = :flag),我认为需要强调的是,我用条件而不是我的列数据检查了我的参数,is null所以如果客户端为我发送Id和flags参数,我将用它们过滤结果否则我只是查询username哪个是我的@PathVariable。
| 归档时间: |
|
| 查看次数: |
15405 次 |
| 最近记录: |