分页和排序存储库查找按属性名称问题



>我有一个酒店实体对象,我需要为其搜索实现分页。我尝试通过PagingAndSortingRepository接口实现它。我在实体中没有列。我的解决方案适用于"目的地代码"和"城市"等某些属性,但是当我尝试针对"酒店代码"和"酒店名称"获取结果时,我在日志中看到计数查询

("Hibernate: select count(hotel0_.destinationcode) as col_0_0_ from Hotel hotel0_ where hotel0_."HOTELNAME"=?") 

仅执行,而不是实际的选择查询,没有错误或异常。对于像findByCity这样的工作方法,我可以在日志中看到两个计数,然后是选择查询。

以上所有 4 个在实体中都是字符串类型。我的主要实体是酒店,它有一个嵌入式的 HotelPk,我认为它就像具有复合键的标准实体一样。 所以我的存储库中有效的方法是

Page<Hotel> findByIdDestinationcode(String destinationCode, Pageable pageRequest); 

Page<Hotel> findByCity(String state, Pageable pageRequest);

而那些不起作用的是

Page<Hotel> findByIdHotelcode(String hotelCode, Pageable pageRequest);
Page<Hotel> findByHotelname(String hotelName, Pageable pageRequest);

我的仓库的签名是

public interface HotelRepository extends JpaRepository<Hotel, HotelPK>, PagingAndSortingRepository<Hotel, HotelPK> {
//...... methods are defined here.
}

任何有关这方面的帮助将不胜感激。

@Entity
@NamedQuery(name="Hotel.findAll", query="SELECT h FROM Hotel h")
public class Hotel implements Serializable {
private static final long serialVersionUID = 1L;
/*@EmbeddedId
private HotelPK id;*/
@Id
private String destinationcode;
private String hotelcode;
@Size(max = 20, message = "Column CITY cannot be more than 20 
characters.")
private String city;
//  @NotNull(message = "Column HOTELNAME cannot be null.")
@Size(max = 50, message = "Column HOTELNAME cannot be more than 50 
characters.")
private String hotelname;
/**
* @return the destinationcode
*/
public String getDestinationcode() {
return destinationcode;
}
/**
* @param destinationcode the destinationcode to set
*/
public void setDestinationcode(String destinationcode) {
this.destinationcode = destinationcode;
}
/**
* @return the hotelcode
*/
public String getHotelcode() {
return hotelcode;
}
/**
* @param hotelcode the hotelcode to set
*/
public void setHotelcode(String hotelcode) {
this.hotelcode = hotelcode;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city) {
this.city = city;
}
/**
* @return the hotelname
*/
public String getHotelname() {
return hotelname;
}
/**
* @param hotelname the hotelname to set
*/
public void setHotelname(String hotelname) {
this.hotelname = hotelname;
}
}
@Service(value = "HotelDao")
public class HotelDaoImpl implements HotelDao {
@Resource
private HotelRepository hotelRepository;
@Override
public Page<Hotel> searchHotel(String hotelCode, String hotelName, String 
destinationCode, Pageable pageRequest) throws Exception {
if(hotelCode!=null){
return hotelRepository.findByHotelcode(Tools.padRight(hotelCode, 
3), pageRequest);
}
if(destinationCode!=null){
return 
hotelRepository.findByDestinationcode(Tools.padRight(destinationCode, 3), 
pageRequest);
}
return hotelRepository.findByHotelname(hotelName, pageRequest);
}
}
public interface HotelRepository extends JpaRepository<Hotel, String> {
Page<Hotel> findByHotelcode(String hotelCode, Pageable pageRequest);
Page<Hotel> findByDestinationcode(String destinationCode, Pageable 
pageRequest);
Page<Hotel> findByHotelname(String hotelname, Pageable pageRequest);
}

没有必要再次扩展PagingAndSortingRepository<Hotel, HotelPK>,因为JpaRepository<Hotel, HotelPK>已经扩展了它。您可以检查其实现。

因此,请像这样定义您的存储库。

public interface HotelRepository extends JpaRepository<Hotel, HotelPK> {
//...... methods are defined here.
}

现在,来到您的问题,您可以执行以下操作。

public Page<Hotel> findAll(Pageable pageable) {
Hotel hotel = new Hotel();
hotel.setHotelCode("HC");
hotel.setHotelName("Hotel");
return hotelRepository.findAll(Example.of(hotel),pageable);
} 

在服务图层中

最新更新