Reputation: 1
I have a hotel entity object and I need to implement pagination for it's search. I tried implementing it through PagingAndSortingRepository interface. I have a no of columns in the entity. My solution works well for some attributes like "destinationcode" and "city" but when I try fetching results against "hotelcode" and "hotelname" I see in log that count query
("Hibernate: select count(hotel0_.destinationcode) as col_0_0_ from Hotel hotel0_ where hotel0_."HOTELNAME"=?")
is executed only and not the actual select query with no error or exception there. In case of working methods like findByCity I can see in log both count followed by Select queries.
All of the above 4 are of type String in the entity. My main entity is Hotel and it has an embeddedid called HotelPk which I think is just like standard entities that have composite keys. so methods in my repository that work are
Page<Hotel> findByIdDestinationcode(String destinationCode, Pageable pageRequest);
and
Page<Hotel> findByCity(String state, Pageable pageRequest);
and the ones that don't work are
Page<Hotel> findByIdHotelcode(String hotelCode, Pageable pageRequest);
Page<Hotel> findByHotelname(String hotelName, Pageable pageRequest);
Signature of my repository is
public interface HotelRepository extends JpaRepository<Hotel, HotelPK>, PagingAndSortingRepository<Hotel, HotelPK> {
//...... methods are defined here.
}
Any help regarding this will be highly appreciated.
@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);
}
Upvotes: 0
Views: 191
Reputation: 2587
There is no need to extend PagingAndSortingRepository<Hotel, HotelPK>
again as JpaRepository<Hotel, HotelPK>
already extends it. You can check its implementation.
So define your repository like this.
public interface HotelRepository extends JpaRepository<Hotel, HotelPK> {
//...... methods are defined here.
}
Now, coming to your question, you can do the following.
public Page<Hotel> findAll(Pageable pageable) {
Hotel hotel = new Hotel();
hotel.setHotelCode("HC");
hotel.setHotelName("Hotel");
return hotelRepository.findAll(Example.of(hotel),pageable);
}
in your Service Layer
Upvotes: 0