Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate Spring数据JPA:返回空列表而不是空列表_Hibernate_Jpa_Spring Data_Jpanel_Spring Data Jpa - Fatal编程技术网

Hibernate Spring数据JPA:返回空列表而不是空列表

Hibernate Spring数据JPA:返回空列表而不是空列表,hibernate,jpa,spring-data,jpanel,spring-data-jpa,Hibernate,Jpa,Spring Data,Jpanel,Spring Data Jpa,我有一个基本的SpringBoot应用程序。使用Spring初始值设定项、JPA、嵌入式Tomcat、Thymeleaf模板引擎,并将包作为可执行JAR文件。 我在扩展自的存储库中定义了此方法 CrudRepository<HotelPrice, Long>, PagingAndSortingRepository<HotelPrice, Long> { 及 这是函数DateUtils.monthlyDate() LocalDate now=LocalDate.now()

我有一个基本的SpringBoot应用程序。使用Spring初始值设定项、JPA、嵌入式Tomcat、Thymeleaf模板引擎,并将包作为可执行JAR文件。 我在扩展自的存储库中定义了此方法

CrudRepository<HotelPrice, Long>, PagingAndSortingRepository<HotelPrice, Long> {

这是函数
DateUtils.monthlyDate()

LocalDate now=LocalDate.now().minusDays(numDaysToSubstract);
返回日期.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant())

我注意到问题中没有什么东西:(更新答案)

  • 无需同时扩展
    crudepository、paging和sortingrepository
    <代码>分页和排序存储库内部扩展
    crudepository
  • 您可以只传递
    Hotel
    (id)的主键,而不是传递整个
    Hotel
    对象。下面给出了它的语法:
    Page getbyhotelandupdatedategreaterthan(长id、日期、可分页)。您可以通过服务发送
    PageRequest
    但您必须在存储库中具有可分页的
  • 当您使用分页时,您将不会得到响应为
    列表
    ,而是会得到
    页面
    对象。要提取
    列表
    ,请使用以下命令:
    List hotelPrice=hotelPricePage.get().getContent()

  • 你应该检查两件事:

  • 返回的方法是什么?它是否与存储库方法中的
    Date
    相同
    getbyhotelandupdatedategreater(Hotel-Hotel,Date-Date,PageRequest-PageRequest)
  • 您真的想要页码1,而不是索引为零的第一页吗?您可以检查页面索引是否是从零开始的

  • 支持可选的。您可以将退货类型更改为
    可选
    。通过这种方式,您可以检查该值是否存在存储库从不返回null,而是始终为空列表!仔细查看堆栈跟踪。异常在参数绑定阶段抛出。参数可能有问题。你能发布
    HotelPrice
    Hotel
    模型吗?我做了,但我得到了错误:分页查询需要有一个可分页的参数@我已经更新了答案。请检查它。仍然有分页查询需要有一个可分页的参数!是的,您必须在存储库层中使用
    Pageable
    对象,但在服务层中,您必须传递
    PageRequest
    对象。服务中:
    hotelPriceRepository.GetByHotelAndUpdateDataGetCreateThan(hotelId,DateUtils.monthlyDate(),新页面请求(1,1,新排序(Sort.Direction.DESC,“price”)和存储库中的
    页面GetByHotelandUpdatedAtegreaterThan(长id、日期、可分页)
    
    List<HotelPrice> getByHotelAndUpdateDateGreaterThan (Hotel hotel, Date date, PageRequest pageRequest);
    
    public List<HotelPrice> getMonthlyMinPriceDate(Hotel hotel) {
            return hotelPriceRepository.getByHotelAndUpdateDateGreaterThan
                    (hotel, DateUtils.monthlyDate(), new PageRequest(1, 1,new Sort(Sort.Direction.DESC, "price")));
        }
    
     java.util.NoSuchElementException
    at java.util.ArrayList$Itr.next(ArrayList.java:860)
    at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1042)
    at org.springframework.data.jpa.repository.query.CriteriaQueryParameterBinder.bind(CriteriaQueryParameterBinder.java:65)
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:101)
    at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:161)
    at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:152)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.invokeBinding(PartTreeJpaQuery.java:236)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.createQuery(PartTreeJpaQuery.java:157)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.doCreateQuery(PartTreeJpaQuery.java:86)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createQuery(AbstractJpaQuery.java:190)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:123)
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:87)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:499)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:477)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
    at com.sun.proxy.$Proxy118.getByHotelAndUpdateDateGreaterThan(Unknown Source)
    at com.booking.backend.service.HotelPriceService.getWeeklyMinPriceDate(HotelPriceService.java:85)
    at com.booking.backend.service.HotelPriceService$$FastClassBySpringCGLIB$$cabd5c58.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
    at com.booking.backend.service.HotelPriceService$$EnhancerBySpringCGLIB$$9dd8e53b.getWeeklyMinPriceDate(<generated>)
    at com.booking.HotelPriceServiceTests.testGetWeeklyMaxPriceDate(HotelPriceServiceTests.java:125)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)Hotel
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
    
    @Entity
    @Table(name="t_Hotel_price")
    public class HotelPrice implements Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        public HotelPrice() {
        }
    
    
        public HotelPrice(Hotel Hotel) {
            this.Hotel = Hotel;
        }
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
    
        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "Hotel_id")
        Hotel Hotel;
    
        float price;
    
        @Column(name = "update_date")
        private Date updateDate;
    
        public Hotel getHotel() {
            return Hotel;
        }
    
        public void setHotel(Hotel Hotel) {
            this.Hotel = Hotel;
        }
    
        public float getPrice() {
            return price;
        }
    
        public void setPrice(float price) {
            this.price = price;
        }
    
    
    
        public Long getId() {
            return id;
        }
    
    
        public void setId(Long id) {
            this.id = id;
        }
    
    
        public Date getUpdateDate() {
            return updateDate;
        }
    
    
        public void setUpdateDate(Date updateDate) {
            this.updateDate = updateDate;
        }
    
    
        @Override
        public String toString() {
            return "HotelPrice [Hotel=" + Hotel + ", price=" + price + ", updateDate=" + updateDate + "]";
        }
    }
    
    @Entity
    @Table(name="t_Hotel")
    public class Hotel  implements Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;    
    
        private String HotelId;
    
        private String symbol;
    
        private float histMaxPrice;
    
        private int numMaxPriceEvents=0;
    
        @Column(name = "update_date")
        @Convert(converter = LocalDateTimeAttributeConverter.class)
        private LocalDateTime histMaxPriceDate;
    
        @OneToMany(mappedBy="Hotel", fetch=FetchType.LAZY, orphanRemoval=true)
        private List<HotelDailyPrice> dailyPrice;
    
        @OneToMany(mappedBy="Hotel", fetch=FetchType.LAZY, orphanRemoval=true)
        private List<HotelPrice> price;
    
        @OneToMany(mappedBy="Hotel", fetch=FetchType.LAZY, orphanRemoval=true)
        private List<HotelPriceSummary> summary;
    
    
        public Hotel() {
            super();
        }
    
        public Hotel(String HotelId) {
            super();
            this.HotelId = HotelId;
        }
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getHotelId() {
            return HotelId;
        }
    
        public void setHotelId(String HotelId) {
            this.HotelId = HotelId;
        }
    
        public float getHistMaxPrice() {
            return histMaxPrice;
        }
    
        public void setHistMaxPrice(float histMaxPrice) {
            this.histMaxPrice = histMaxPrice;
        }
    
        public LocalDateTime getHistMaxPriceDate() {
            return histMaxPriceDate;
        }
    
        public void setHistMaxPriceDate(LocalDateTime histMaxPriceDate) {
            this.histMaxPriceDate = histMaxPriceDate;
        }
    
        public String getSymbol() {
            return symbol;
        }
    
        public void setSymbol(String symbol) {
            this.symbol = symbol;
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((HotelId == null) ? 0 : HotelId.hashCode());
            result = prime * result + ((id == null) ? 0 : id.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Hotel other = (Hotel) obj;
            if (HotelId == null) {
                if (other.HotelId != null)
                    return false;
            } else if (!HotelId.equals(other.HotelId))
                return false;
            if (id == null) {
                if (other.id != null)
                    return false;
            } else if (!id.equals(other.id))
                return false;
            return true;
        }
    
        @Override
        public String toString() {
            return "Hotel [id=" + id + ", HotelId=" + HotelId + ", histMaxPrice=" + histMaxPrice
                    + ", histMaxPriceDate=" + histMaxPriceDate + "]";
        }
    
        public int getNumMaxPriceEvents() {
            return numMaxPriceEvents;
        }
    
        public void setNumMaxPriceEvents(int numMaxPriceEvents) {
            this.numMaxPriceEvents = numMaxPriceEvents;
        }
    
        public List<HotelDailyPrice> getDailyPrice() {
            return dailyPrice;
        }
    
        public void setDailyPrice(List<HotelDailyPrice> dailyPrice) {
            this.dailyPrice = dailyPrice;
        }
    
        public List<HotelPrice> getPrice() {
            return price;
        }
    
        public void setPrice(List<HotelPrice> price) {
            this.price = price;
        }
    
        public List<HotelPriceSummary> getSummary() {
            return summary;
        }
    
        public void setSummary(List<HotelPriceSummary> summary) {
            this.summary = summary;
        }
    }
    
    Paging query needs to have a Pageable parameter!