我试图在SpringBoot中使用mongock开发两个类之间的关系,但无法在这些类之间创建一个单独的关系。
我使用@dbref来建立关系
类别.java
package com.test.model;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document("category")
public class Category {
@Id
private Integer id;
private String name;
@DBRef
private List<Requirement> requirements ;
}
需求.java
package com.test.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document("requirements")
public class Requirement {
@Id
private int Id;
private String subcls;
private String standard;
}
为了在mongodb中存储数据,我使用了以下配置类
数据库更改日志.java
package com.test.config;
import org.bson.Document;
import com.github.cloudyrock.mongock.ChangeLog;
import com.github.cloudyrock.mongock.ChangeSet;
import com.github.cloudyrock.mongock.driver.mongodb.springdata.v3.decorator.impl.MongockTemplate;
import com.mongodb.client.MongoCollection;
import com.test.config.Migration;
@ChangeLog
public class DatabaseChangeLog {
@ChangeSet(order="001", id="seedDatabase", author="testuser")
public void seedDatabase(MongockTemplate db) {
MongoCollection<Document> mycollection = db.getCollection("category");
for(String json : Migration.Category) {
mycollection.insertOne(Document.parse(json));
}
}
@ChangeSet(order="002", id="seedDatabase2", author="testuser")
public void seedDatabaseRequirement(MongockTemplate db) {
MongoCollection<Document> mycollection = db.getCollection("requirements");
for(String json : Migration.Requirements) {
System.out.println(json);
mycollection.insertOne(Document.parse(json));
}
}
}
迁移.java
package com.test.config;
public class Migration {
public static final String[] Category = {
"{'_id': 1,'name':'Data Migration'}",
"{'_id': 2,'name':'Data Recovery'}"
};
public static final String[] Requirements = {
"{'_id': 1, 'subcls': 'sub-class DM', 'standard' : 'DM_Standard','category': {'$ref': 'category', '$id': 1}}",
"{'_id': 2, 'subcls': 'sub-class DM', 'standard' : 'DM_Standard', 'category': {'$ref': 'category', '$id': 1}}",
"{'_id': 3, 'subcls': 'sub-class DR', 'standard' : 'DR_Standard', 'category': {'$ref': 'category', '$id': 2}}"
};
}
应用程序属性
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=testdb
mongock.change-logs-scan-package=com.test.config
从上面的代码,我已经能够创建数据库及其集合,即类别和要求
问题是当我试图在 Package 类的帮助下调用@restcontroller时。我想从api中获取类别数据以及相应的需求数据。
categorycontroller.java包com.test.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.test.model.Category;
import com.test.model.Requirement;
import com.test.CategoryRepository;
@RestController
@RequestMapping("/api")
public class CategoryController {
@Autowired
private CategoryRepository catRepo;
@GetMapping(value="/categories", produces={"application/json"})
public Iterable<CategoryGet> getAllCategories() {
Iterable<Category> categories = catRepo.findAll();
List<CategoryGet> gets = new ArrayList<CategoryGet>();
for(Category category: categories) {
gets.add( new CategoryGet(category) );
}
return gets;
}
}
Package 类
分类
package com.test.controller;
import java.util.ArrayList;
import java.util.List;
import com.test.model.Category;
import com.test.model.Requirement;
public class CategoryGet implements java.io.Serializable {
private int id;
private String name;
private List<RequirementGet> requirements = new ArrayList<RequirementGet>();
public CategoryGet() {
}
public CategoryGet(Category cat) {
this.name = cat.getName();
for(Requirement req: cat.getRequirements()) {
this.requirements.add(new RequirementGet(req));
}
}
public String getName() {
return name;
}
public List<RequirementGet> getRequirements() {
return requirements;
}
}
需求获取.java
package com.test.controller;
import com.test.model.Requirement;
public class RequirementGet implements java.io.Serializable{
private int id;
private String subcls;
private String standard;
private String category;
public RequirementGet() {}
public RequirementGet(Requirement req) {
super();
this.id = req.getId();
this.subcls = req.getSubcls();
this.standard = req.getStandard();
}
public int getId() {
return id;
}
public String getSubcls() {
return subcls;
}
public String getStandard() {
return standard;
}
}
在我得到的错误之后
在此处输入图像描述
相应类别的要求为空。
如果需要其他信息,请务必告诉我。
暂无答案!
目前还没有任何答案,快来回答吧!