我是 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") …Run Code Online (Sandbox Code Playgroud)