Java 在Spring中使用类进行查询
我正在尝试做这个查询Java 在Spring中使用类进行查询,java,spring-boot,jpa,intellij-idea,Java,Spring Boot,Jpa,Intellij Idea,我正在尝试做这个查询 列出findProductByCategory\u CategoryNameAndPriceBetween(字符串类别、双最小值、双最大值) 但是查询返回的是一个空列表。我做错了什么?我还尝试在类别和类别名称之间不使用下划线。这是类别和产品: @Entity public class Category { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="ID
列出findProductByCategory\u CategoryNameAndPriceBetween(字符串类别、双最小值、双最大值)代码>
但是查询返回的是一个空列表。我做错了什么?我还尝试在类别和类别名称之间不使用下划线。这是类别和产品:
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="ID")
private Long id;
@Column(name = "NAME")
private String categoryName;
@OneToMany(mappedBy = "category", fetch = FetchType.EAGER)
private List<Product> products;
...
}
这就是我实现存储库的方式:
@Repository
public interface ProductDao extends JpaRepository<Product,Long> {
...
@存储库
公共接口产品DAO扩展了JpaRepository{
...
根据您的方法应该是:
List<Product> findProductByCategoryNameAndPriceBetween(String category, double min, double max)
列出findProductByCategoryNameAndPriceBetween(字符串类别、双最小值、双最大值)
假设categoryName和price是在Category类中定义的。使用下面的实现,您的查询就可以了
请注意,我添加了一个替代实现:
List<Product> findByCategoryAndPriceBetween(Category category, double min, double max);
产品类别
package no.mycompany.myapp.misc;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
// cannot use @Data here because of circular ref
@Getter
@Setter
@Entity
@Table(name="PRODUCT")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="ID")
private Long id;
@Column(name="PRICE")
private Double price;
@Column(name="BRAND")
private String brand;
@Column(name="MODEL")
private String model;
//@Lob
@Column(name="DESCRIPTION")
private String description;
@Column(name = "IMAGE_URL")
private String imageUrl;
@ManyToOne
@JoinColumn(name = "CATEGORY_ID", referencedColumnName = "ID")
private Category category;
}
产品刀
public interface ProductDao extends JpaRepository<Product, Long> {
List<Product> findProductByCategory_CategoryNameAndPriceBetween(String category, double min, double max);
List<Product> findByCategoryAndPriceBetween(Category category, double min, double max);
}
请同时发布您的JPA类!我在任何地方都看不到价格。请注意,第一步通常是确认它们符合您期望的逻辑。价格在类别产品中,而categoryName在类别类中
package no.mycompany.myapp.misc;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
// cannot use @Data here because of circular ref
@Getter
@Setter
@Entity
@Table(name="PRODUCT")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="ID")
private Long id;
@Column(name="PRICE")
private Double price;
@Column(name="BRAND")
private String brand;
@Column(name="MODEL")
private String model;
//@Lob
@Column(name="DESCRIPTION")
private String description;
@Column(name = "IMAGE_URL")
private String imageUrl;
@ManyToOne
@JoinColumn(name = "CATEGORY_ID", referencedColumnName = "ID")
private Category category;
}
public interface ProductDao extends JpaRepository<Product, Long> {
List<Product> findProductByCategory_CategoryNameAndPriceBetween(String category, double min, double max);
List<Product> findByCategoryAndPriceBetween(Category category, double min, double max);
}
package no.mycompany.myapp.misc;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ActiveProfiles;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DataJpaTest
public class ProductDaoTest {
@Autowired
ProductDao productDao;
@Autowired
TestEntityManager testEntityManager;
@Test
public void findByCategoryAndPriceBetween_singleCategoryWithSingleProductInDb_expectSingleProduct() {
var category = createCategoryWithSingleProduct();
testEntityManager.persist(category);
var result = productDao.findByCategoryAndPriceBetween(category, 0.0, 2.0);
assertThat(result.size()).isEqualTo(1);
}
@Test
public void findByCategoryAndPriceBetween_singleCategoryWithSingleProductInDb_expectEmptyResult() {
var category = createCategoryWithSingleProduct();
testEntityManager.persist(category);
var result = productDao.findByCategoryAndPriceBetween(category, 5.0, 6.0);
assertThat(result.size()).isEqualTo(0);
}
@Test
public void findProductByCategory_CategoryNameAndPriceBetween_singleCategoryWithSingleProductInDb_expectSingleProduct() {
var category = createCategoryWithSingleProduct();
testEntityManager.persist(category);
var result = productDao.findProductByCategory_CategoryNameAndPriceBetween(category.getCategoryName(), 0.0, 2.0);
assertThat(result.size()).isEqualTo(1);
}
private static Category createCategoryWithSingleProduct() {
Product product = new Product();
product.setPrice(1.00);
Category category = new Category();
category.setCategoryName("test");
category.getProducts().add(product);
product.setCategory(category);
return category;
}
}