Spring Data JPA: вернуть пустой список вместо NULLJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Spring Data JPA: вернуть пустой список вместо NULL

Сообщение Anonymous »

У меня есть базовое приложение Springboot. Используя пружинный инициализатор, JPA, встроенный Tomcat, Thymeleaf -двигатель и пакет в качестве файла исполняемого JAR.
У меня есть этот метод, определенный в репозитории, который простирается от < /p>

CrudRepository, PagingAndSortingRepository {
< /code>

Это метод < /p>

List getByHotelAndUpdateDateGreaterThan (Hotel hotel, Date date, PageRequest pageRequest);
< /code>

и служба: < /p>

public List getMonthlyMinPriceDate(Hotel hotel) {
return hotelPriceRepository.getByHotelAndUpdateDateGreaterThan
(hotel, DateUtils.monthlyDate(), new PageRequest(1, 1,new Sort(Sort.Direction.DESC, "price")));
}
< /code>

Но когда я запускаю тест JUNIT, я получил эту ошибку: < / p > < b r / > < b r / > < c o d e > j a v a . u t i l . N o S u c h E l e m e n t E x c e p t i o n < b r / > a t j a v a . u t i l . A r r a y L i s t $ I t r . n e x t ( A r r a y L i s t . j a v a : 8 6 0 ) < b r / > a t j a v a . u t i l . C o l l e c t i o n s $ U n m o d i f i a b l e C o l l e c t i o n $ 1 . n e x t ( C o l l e c t i o n s . j a v a : 1 0 4 2 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . C r i t e r i a Q u e r y P a r a m e t e r B i n d e r . b i n d ( C r i t e r i a Q u e r y P a r a m e t e r B i n d e r . j a v a : 6 5 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . P a r a m e t e r B i n d e r . b i n d ( P a r a m e t e r B i n d e r . j a v a : 1 0 1 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . P a r a m e t e r B i n d e r . b i n d A n d P r e p a r e ( P a r a m e t e r B i n d e r . j a v a : 1 6 1 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . P a r a m e t e r B i n d e r . b i n d A n d P r e p a r e ( P a r a m e t e r B i n d e r . j a v a : 1 5 2 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . P a r t T r e e J p a Q u e r y $ Q u e r y P r e p a r e r . i n v o k e B i n d i n g ( P a r t T r e e J p a Q u e r y . j a v a : 2 3 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . P a r t T r e e J p a Q u e r y $ Q u e r y P r e p a r e r . c r e a t e Q u e r y ( P a r t T r e e J p a Q u e r y . j a v a : 1 5 7 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . P a r t T r e e J p a Q u e r y . d o C r e a t e Q u e r y ( P a r t T r e e J p a Q u e r y . j a v a : 8 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . A b s t r a c t J p a Q u e r y . c r e a t e Q u e r y ( A b s t r a c t J p a Q u e r y . j a v a : 1 9 0 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . J p a Q u e r y E x e c u t i o n $ C o l l e c t i o n E x e c u t i o n . d o E x e c u t e ( J p a Q u e r y E x e c u t i o n . j a v a : 1 2 3 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . J p a Q u e r y E x e c u t i o n . e x e c u t e ( J p a Q u e r y E x e c u t i o n . j a v a : 8 7 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . A b s t r a c t J p a Q u e r y . d o E x e c u t e ( A b s t r a c t J p a Q u e r y . j a v a : 1 1 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . q u e r y . A b s t r a c t J p a Q u e r y . e x e c u t e ( A b s t r a c t J p a Q u e r y . j a v a : 1 0 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . r e p o s i t o r y . c o r e . s u p p o r t . R e p o s i t o r y F a c t o r y S u p p o r t $ Q u e r y E x e c u t o r M e t h o d I n t e r c e p t o r . d o I n v o k e ( R e p o s i t o r y F a c t o r y S u p p o r t . j a v a : 4 9 9 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . r e p o s i t o r y . c o r e . s u p p o r t . R e p o s i t o r y F a c t o r y S u p p o r t $ Q u e r y E x e c u t o r M e t h o d I n t e r c e p t o r . i n v o k e ( R e p o s i t o r y F a c t o r y S u p p o r t . j a v a : 4 7 7 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . R e f l e c t i v e M e t h o d I n v o c a t i o n . p r o c e e d ( R e f l e c t i v e M e t h o d I n v o c a t i o n . j a v a : 1 7 9 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . p r o j e c t i o n . D e f a u l t M e t h o d I n v o k i n g M e t h o d I n t e r c e p t o r . i n v o k e ( D e f a u l t M e t h o d I n v o k i n g M e t h o d I n t e r c e p t o r . j a v a : 5 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . R e f l e c t i v e M e t h o d I n v o c a t i o n . p r o c e e d ( R e f l e c t i v e M e t h o d I n v o c a t i o n . j a v a : 1 7 9 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . t r a n s a c t i o n . i n t e r c e p t o r . T r a n s a c t i o n I n t e r c e p t o r $ 1 . p r o c e e d W i t h I n v o c a t i o n ( T r a n s a c t i o n I n t e r c e p t o r . j a v a : 9 9 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . t r a n s a c t i o n . i n t e r c e p t o r . T r a n s a c t i o n A s p e c t S u p p o r t . i n v o k e W i t h i n T r a n s a c t i o n ( T r a n s a c t i o n A s p e c t S u p p o r t . j a v a : 2 8 2 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . t r a n s a c t i o n . i n t e r c e p t o r . T r a n s a c t i o n I n t e r c e p t o r . i n v o k e ( T r a n s a c t i o n I n t e r c e p t o r . j a v a : 9 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . R e f l e c t i v e M e t h o d I n v o c a t i o n . p r o c e e d ( R e f l e c t i v e M e t h o d I n v o c a t i o n . j a v a : 1 7 9 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a o . s u p p o r t . P e r s i s t e n c e E x c e p t i o n T r a n s l a t i o n I n t e r c e p t o r . i n v o k e ( P e r s i s t e n c e E x c e p t i o n T r a n s l a t i o n I n t e r c e p t o r . j a v a : 1 3 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . R e f l e c t i v e M e t h o d I n v o c a t i o n . p r o c e e d ( R e f l e c t i v e M e t h o d I n v o c a t i o n . j a v a : 1 7 9 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . s u p p o r t . C r u d M e t h o d M e t a d a t a P o s t P r o c e s s o r $ C r u d M e t h o d M e t a d a t a P o p u l a t i n g M e t h o d I n t e r c e p t o r . i n v o k e ( C r u d M e t h o d M e t a d a t a P o s t P r o c e s s o r . j a v a : 1 3 3 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p.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()
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()
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)
< /code>

можно было бы вернуть пустой список вместо нуля ???? < /p>

Я также установил приложение. Свойство Spring.jpa.properties.hibernate.format_sql = true < /code> < /p>

Но я не вижу, чтобы какой -либо SQL в консоли < /p>

@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 + "]";
}
}
< /code>

и < /p>

@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 dailyPrice;

@OneToMany(mappedBy="Hotel", fetch=FetchType.LAZY, orphanRemoval=true)
private List price;

@OneToMany(mappedBy="Hotel", fetch=FetchType.LAZY, orphanRemoval=true)
private List 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 getDailyPrice() {
return dailyPrice;
}

public void setDailyPrice(List dailyPrice) {
this.dailyPrice = dailyPrice;
}

public List getPrice() {
return price;
}

public void setPrice(List price) {
this.price = price;
}

public List getSummary() {
return summary;
}

public void setSummary(List summary) {
this.summary = summary;
}
}
< /code>

Я также пытался использовать страницу < /code> Но затем я получил ошибку: < /p>

Paging query needs to have a Pageable parameter!
< /code>

Это функция dateutils.monthlydate () < /code> < /p>

localdate now = localdate.now (). Minusdays (numdaystosubstract);
дата возврата.from (now.atstartofday (ZoneId.SystemDefault ()). ToInstant ()); < /p>

Подробнее здесь: https://stackoverflow.com/questions/490 ... ad-of-null
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Postgres ement varchar как null вместо пустой строки в Spring Data JPA
    Anonymous » » в форуме JAVA
    0 Ответы
    2 Просмотры
    Последнее сообщение Anonymous
  • Postgres ement varchar как null вместо пустой строки в Spring Data JPA
    Anonymous » » в форуме JAVA
    0 Ответы
    3 Просмотры
    Последнее сообщение Anonymous
  • Android Monhofit Post Type Data Data Wations Data Null Null
    Anonymous » » в форуме Android
    0 Ответы
    46 Просмотры
    Последнее сообщение Anonymous
  • Android Monhofit Post Type Data Data Wations Data Null Null
    Anonymous » » в форуме Android
    0 Ответы
    38 Просмотры
    Последнее сообщение Anonymous
  • Как фильтровать выборку Hibernate? Spring Data JPA, отношение ManyToMany с новой сущностью, спецификации JPA
    Anonymous » » в форуме JAVA
    0 Ответы
    30 Просмотры
    Последнее сообщение Anonymous

Вернуться в «JAVA»