首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PagingAndSortingRepository findByattributename问题

PagingAndSortingRepository findByattributename问题
EN

Stack Overflow用户
提问于 2018-09-08 04:57:00
回答 1查看 133关注 0票数 0

我有一个酒店实体对象,我需要为它的搜索实现分页。我试着通过PagingAndSortingRepository接口实现它。我在实体中有一个列的编号。我的解决方案适用于一些属性,比如"destinationcode“和"city”,但是当我尝试根据“My”和“My”获取结果时,我在日志中看到了计数查询

代码语言:javascript
运行
复制
 ("Hibernate: select count(hotel0_.destinationcode) as col_0_0_ from Hotel hotel0_ where hotel0_."HOTELNAME"=?") 

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

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

代码语言:javascript
运行
复制
Page<Hotel> findByIdDestinationcode(String destinationCode, Pageable pageRequest); 

代码语言:javascript
运行
复制
Page<Hotel> findByCity(String state, Pageable pageRequest);

不起作用的是

代码语言:javascript
运行
复制
Page<Hotel> findByIdHotelcode(String hotelCode, Pageable pageRequest);
Page<Hotel> findByHotelname(String hotelName, Pageable pageRequest);

我的存储库的签名是

代码语言:javascript
运行
复制
public interface HotelRepository extends JpaRepository<Hotel, HotelPK>, PagingAndSortingRepository<Hotel, HotelPK> {
//...... methods are defined here.
}

任何关于这方面的帮助都将受到高度的感谢。

代码语言:javascript
运行
复制
@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);
}
EN

回答 1

Stack Overflow用户

发布于 2018-09-08 12:59:02

不需要再次扩展PagingAndSortingRepository<Hotel, HotelPK>,因为JpaRepository<Hotel, HotelPK>已经扩展了它。你可以检查它的实现。

所以像这样定义你的存储库。

代码语言:javascript
运行
复制
public interface HotelRepository extends JpaRepository<Hotel, HotelPK> {
//...... methods are defined here.
}

现在,对于您的问题,您可以执行以下操作。

代码语言:javascript
运行
复制
  public Page<Hotel> findAll(Pageable pageable) {
    Hotel hotel = new Hotel();
    hotel.setHotelCode("HC");
    hotel.setHotelName("Hotel");
    return hotelRepository.findAll(Example.of(hotel),pageable);
  } 

在你的服务层

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52229551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档