Spring Boot无法从复合主键的查询中返回任何数据。

huangapple go评论107阅读模式
英文:

Spring Boot does not return any data from query for composite primary key

问题

我有一个包含 ProductId 和 Product 类的列表。我还有一个用于 Product 类的 ProductRepository。ProductId 是一个复合主键。我应该如何编写一个针对以下结构的复合 id 列表的条件查询:

@Data
class ProductId{
    Long productId;
    Long categoryCode;
}
          
@Data
@Document(collection = "products")
class Product {
                
    @Id
    ProductId id; 
               
    Long productName: string;
    Long category;
    Long quantityInStock;
                
    @PersistenceCreator
    Product(ProductId id,Long productName,Long category,Long quantityInStock){
        this.id = id;
        this.productName= productName;
        this.category = category;
        this.quantityInStock = quantityInStock;

    }
}         

这是仓库的定义:

public interface ProductRepository extends MongoRepository<Product, ProductId> {
   
}

当我使用以下代码创建查询时:

Product id = productRepository.findById(id); // 返回 Optional.empty

这也不返回任何数据,返回空列表:

public List<Product> find(List<ProductId> ids) {                 
    Query query = new Query();
    Criteria criteria = Criteria.where("id").in(ids);
    query.addCriteria(criteria);
    List<Product> results = mongoTemplate.find(query, Product.class);
    return results;
}

这是 Studio 3T 中的视图:

{
    "_id": {
         "productId": 872223334,
         "categoryCode": 4
    },
    "productName": "Decantor Jacket",
    "category": 'Plaid-Item',
    "quantityInStock": 4001
}

我该如何修复这些查询?

英文:

I have a list classes ProductId and Product. I also have a ProductRepository for the Product class. The ProductId is a composite primary key.
How can I write a criteria query for a list of composite ids for the following structure:

@Data
class ProductId{
    Long productId;
    Long categoryCode;
}
          
@Data
@Document(collection = &quot;products&quot;)
class Product {
                
    @Id
    ProductId id; 
               
    Long productName: string;
    Long category;
    Long quantityInStock;
                
    @PersistenceCreator
    Product(ProductId id,Long productName,Long category,Long quantityInStock){
        this.id = id;
        this.productName= productName;
        this.category = category;
        this.quantityInStock = quantityInStock;

    }
}         

Here is the repository definition


public interface ProductRepository extends MongoRepository&lt;Product, ProductId&gt; {
   
}

When I create a query using

Product id = productRepository.findById(id); // Returns Optional.empty

This also does not return any data., returns empty list


public List&lt;Product&gt; find(List&lt;ProductId&gt; ids) {                 
    Query query = new Query();
    Criteria criteria = Criteria.where(&quot;id&quot;).in(ids);
    query.addCriteria(criteria);
    List&lt;Product&gt; results = mongoTemplate.find(query, Product.class);
    return results;
}

Here is the view from Studio 3T


{
    &quot;_id&quot;: {
         &quot;productId&quot;: 872223334,
         &quot;categoryCode&quot;: 4
    },
    &quot;productName&quot;: &quot;Decantor Jacket&quot;,
    &quot;category&quot;: &#39;Plaid-Item&#39;,
    &quot;quantityInStock&quot;: 4001
}

How can I fix these queries?

答案1

得分: 0

对于复合键,您需要按照以下方式构建查询:

db.product.aggregate([
  {
    $match: {
      $or: [
        {
          $and: [
            {
              "_id.productId": 872223334
            },
            {
              "_id.categoryCode": 4
            }
          ]
        },
        {
          $and: [
            {
              "_id.productId": 872223335
            },
            {
              "_id.categoryCode": 5
            }
          ]
        }
      ]
    }
  }
])

MongoPlayground

英文:

For composite keys, you need to build your query this way:

db.product.aggregate([
  {
    $match: {
      $or: [
        {
          $and: [
            {
              &quot;_id.productId&quot;: 872223334
            },
            {
              &quot;_id.categoryCode&quot;: 4
            }
          ]
        },
        {
          $and: [
            {
              &quot;_id.productId&quot;: 872223335
            },
            {
              &quot;_id.categoryCode&quot;: 5
            }
          ]
        }
      ]
    }
  }
])

MongoPlayground

huangapple
  • 本文由 发表于 2023年8月9日 05:32:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定