我有一个酒店实体对象,需要对其进行实现分页。我尝试通过PagingAndSortingRepository接口实现它。我在实体中没有列。我的解决方案适用于“目的地代码”和“城市”等某些属性,但是当我尝试针对“酒店代码”和“酒店名称”获取结果时,我会在日志中看到计数查询
("Hibernate: select count(hotel0_.destinationcode) as col_0_0_ from Hotel hotel0_ where hotel0_."HOTELNAME"=?")
仅执行,而不是实际的选择查询,没有错误或异常。对于像findByCity这样的工作方法,我可以在日志中看到两个计数,然后是Select查询。
以上所有4个实体中的类型均为String。我的主要实体是酒店,它有一个称为HotelPk的嵌入式ID,我认为它就像具有复合密钥的标准实体一样。 所以我的存储库中有效的方法是
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);
}
答案 0 :(得分:0)
由于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);
}
在您的服务层