我的任務是讓所有東西都在從最低價格到最高價格的范圍內。根據我的嘗試,我嘗試將 List 更改為 Set 以僅存盤唯一物件,但這沒有幫助,我還嘗試在兩個物體類中創建 FetchType.EAGER,但這也沒有幫助
控制器
@GetMapping("/get-item")
@PreAuthorize(RoleExpressions.PERMIT_ALL)
public Page<Item> getItems(BigDecimal minPrice, BigDecimal maxPrice){
return itemRepository.findAllByPrices(
priceRepository.findAllByPriceBetween(minPrice, maxPrice),
PageRequest.of(0, 10, Sort.Direction.DESC));
}
物品儲存庫
public interface ItemRepository extends JpaRepository<Item, Long>, JpaSpecificationExecutor<Item> {
Page<Item> findAllByPrices(List<Price> prices, Pageable pageable);
}
價格庫
public interface PriceRepository extends JpaRepository<Price, Long>{
List<Price> findAllByPriceBetween(BigDecimal minPrice, BigDecimal maxPrice);
}
物品物體
@Data
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "item")
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "item_id")
@SequenceGenerator(name = "item_id", sequenceName = "item_id_seq")
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "thumbnail", nullable = false)
private String thumbnail;
@Column(name = "photo", nullable = false)
private String photo;
@Column(name = "short_description", nullable = false)
private String shortDescription;
@Column(name = "description", nullable = false)
private String description;
@ManyToOne
@JoinColumn(nullable = false)
private Category category;
@OneToMany(mappedBy = "item", cascade = CascadeType.REMOVE)
@Builder.Default
private List<Price> prices = new ArrayList<>();
}
價格物體
@Entity
@Table(name = "price_information")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Price {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "news_id")
@SequenceGenerator(name = "news_id", sequenceName = "news_id_seq")
private Long id;
@Column(name = "price", nullable = false)
private BigDecimal price;
@Column(name = "date", nullable = false)
private LocalDateTime date;
@ManyToOne
@JoinColumn(nullable = false)
@EqualsAndHashCode.Exclude
private Item item;
}
例外
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemController' defined in file [D:\issue_operation_img_testing\angulartrainee-backend\build\classes\java\main\com\elinext\angulartrainee\controller\ItemController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemServiceImpl' defined in file [D:\issue_operation_img_testing\angulartrainee-backend\build\classes\java\main\com\elinext\angulartrainee\service\impl\ItemServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemRepository' defined in com.elinext.angulartrainee.repository.ItemRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.domain.Page com.elinext.angulartrainee.repository.ItemRepository.findAllByPrices(java.util.List,org.springframework.data.domain.Pageable)! Operator SIMPLE_PROPERTY on prices requires a scalar argument, found interface java.util.List in method public abstract org.springframework.data.domain.Page com.elinext.angulartrainee.repository.ItemRepository.findAllByPrices(java.util.List,org.springframework.data.domain.Pageable).
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:799)
Caused by: java.lang.IllegalStateException: Operator SIMPLE_PROPERTY on prices requires a scalar argument, found interface java.util.List in method public abstract org.springframework.data.domain.Page com.elinext.angulartrainee.repository.ItemRepository.findAllByPrices(java.util.List,org.springframework.data.domain.Pageable).
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.throwExceptionOnArgumentMismatch(PartTreeJpaQuery.java:171)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.validate(PartTreeJpaQuery.java:147)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:90)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.<init>(QueryExecutorMethodInterceptor.java:84)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:332)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$4(RepositoryFactoryBeanSupport.java:294)
at org.springframework.data.util.Lazy.getNullable(Lazy.java:211)
at org.springframework.data.util.Lazy.get(Lazy.java:95)
uj5u.com熱心網友回復:
您的存盤庫方法應更改為:
Page<Item> findAllByPricesIn(List<Price> prices, Pageable pageable);
如果目標是在您作為引數傳遞的價格集合中找到具有價格的“所有專案”。
使用INwhich 是Spring Data JPA supported keywords in methods.
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/362789.html
