首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按其他实体搜索实体作为条件

问按其他实体搜索实体作为条件
EN

Stack Overflow用户
提问于 2018-12-01 06:59:06
回答 2查看 42关注 0票数 0

我在实体之间有一个简单的关系:

代码语言:javascript
复制
class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String name;
    private double calories;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn
    private Category category;
}

class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(unique = true)
    private String name;

    Category(String name) {
        this.name = name;
    }
}

我正在使用下面的存储库

代码语言:javascript
复制
interface ProductRepository extends Repository<Product, Long> {
    Product save(Product product);
    Page<Product> findAll(Pageable pageable);
    Page<Product> findByCategory(Pageable pageable, Category category);
    void delete(Product product);
}

它在facade中调用,如下所示

代码语言:javascript
复制
 public Page<ProductDTO> getProductsByCategory(Pageable pageable, String categoryName) {
        return productRepository.findByCategory(pageable, dtoConverter.toCategory(categoryName))
                .map(Product::toDTO);
    }

在dtoConverter中

代码语言:javascript
复制
Category toCategory(String categoryName) {
        return new Category(categoryName);
    }

最终将我们引向Controller

代码语言:javascript
复制
@GetMapping("/findCategory")
    Page<ProductDTO> getProductsByCategory(Pageable pageable, @RequestParam String categoryName) {
        return productFacade.getProductsByCategory(pageable, categoryName);
    }

我得到了非常类似的方法来获得和创造新的产品,它是有效的,但一旦我尝试按照我上面描述的方式按类别查找产品,我就得到了

代码语言:javascript
复制
{
    "timestamp": "2018-11-30T22:57:29.660+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/products/findCategory=fruit"
}

尽管如此,我确信有这类产品存储在数据库中(我发现它们直接查看mysql并使用findAll端点)。有人能给我解释一下这里出了什么问题吗?

EN

回答 2

Stack Overflow用户

发布于 2018-12-01 07:32:11

您在下面的代码片段中遇到了问题。

代码语言:javascript
复制
Category toCategory(String categoryName) {
    return new Category(categoryName);
}

你不能仅仅创建一个新的对象。您需要返回一个引用数据库表的对象。因此,首先需要从数据库中检索类别对象,然后将其返回。

因此,您将创建一个CategoryRepository:

代码语言:javascript
复制
public interface CategoryRepository extends Repository<Category,Long> {
    Category findByName(String name);
}

然后在你的方法中:

代码语言:javascript
复制
Category toCategory(String categoryName) {
    return categoryRepository.findByName(categoryName);
}

附注:您可以扩展JpaRepository,而不是Repository,这将提供几个方便的方法。

票数 1
EN

Stack Overflow用户

发布于 2018-12-06 23:17:45

由于您的类别名称是唯一的,因此您可以将存储库方法更改为

代码语言:javascript
复制
interface ProductRepository extends Repository<Product, Long> {
    Product save(Product product);
    Page<Product> findAll(Pageable pageable);
    Page<Product> findByCategory_Name(Pageable pageable, String categoryName);
    void delete(Product product);
}

并在您的getProductsByCategory方法中使用它

代码语言:javascript
复制
public Page<ProductDTO> getProductsByCategory(Pageable pageable, String categoryName) 
{
    return productRepository.findByCategory_Name(pageable, categoryName))
            .map(Product::toDTO);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53566049

复制
相关文章

相似问题

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