英文:
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 = "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;
}
}
Here is the repository definition
public interface ProductRepository extends MongoRepository<Product, ProductId> {
}
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<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;
}
Here is the view from Studio 3T
{
"_id": {
"productId": 872223334,
"categoryCode": 4
},
"productName": "Decantor Jacket",
"category": 'Plaid-Item',
"quantityInStock": 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
}
]
}
]
}
}
])
英文:
For composite keys, you need to build your query this way:
db.product.aggregate([
{
$match: {
$or: [
{
$and: [
{
"_id.productId": 872223334
},
{
"_id.categoryCode": 4
}
]
},
{
$and: [
{
"_id.productId": 872223335
},
{
"_id.categoryCode": 5
}
]
}
]
}
}
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论