在java中,如何将@DbRef与具有列表作为值的映射一起使用?
这就是我试图做的,但它不起作用在java中,如何将@DbRef与具有列表作为值的映射一起使用?,java,spring,mongodb,spring-data-mongodb,Java,Spring,Mongodb,Spring Data Mongodb,这就是我试图做的,但它不起作用 @DBRef( db = "employee") private Map<Role, List<Employee>> rolesByGroup; @DBRef(db=“employee”) 私人地图角色组; 您应该这样做: public class EmployeeList { @DBRef public List<Employee> employees = new ArrayList<>(); }
@DBRef( db = "employee")
private Map<Role, List<Employee>> rolesByGroup;
@DBRef(db=“employee”)
私人地图角色组;
您应该这样做:
public class EmployeeList {
@DBRef public List<Employee> employees = new ArrayList<>();
}
private Map<Role, EmployeeList> rolesByGroup;
公共类员工列表{
@DBRef public List employees=new ArrayList();
}
私人地图角色组;
您应该这样做:
public class EmployeeList {
@DBRef public List<Employee> employees = new ArrayList<>();
}
private Map<Role, EmployeeList> rolesByGroup;
公共类员工列表{
@DBRef public List employees=new ArrayList();
}
私人地图角色组;
我不知道它是否实际存在,但目前我使用弹簧转换器对此进行了解决。如果对象不太复杂,则可以执行相同的操作,或者改进并使其更通用:
@WritingConverter
public class ChildWriteConverter implements Converter<Child, Document> {
@Override
public Document convert(Child source) {
//TODO: Think about generic solution
Document document = new Document();
document.put("_id", source.getId());
Map<String, List<Child>> children = source.getChildren();
Document subdocument = new Document();
if(children != null) {
children.forEach((key, childList) -> {
List<DBRef> dbList = new ArrayList<>(childList.size());
childList.forEach(child -> {
DBRef dbref = new DBRef("Child", child.getId());
dbList.add(dbref);
});
subdocument.put(key, dbList);
});
document.put("children", subdocument);
}
return document;
}
}
@WritingConverter
公共类ChildWriteConverter实现转换器{
@凌驾
公共文档转换(子源){
//TODO:考虑通用解决方案
文档=新文档();
document.put(“_id”,source.getId());
Map children=source.getChildren();
文档子文档=新文档();
如果(子项!=null){
children.forEach((键,childList)->{
List dbList=newarraylist(childList.size());
childList.forEach(child->{
DBRef DBRef=newdbref(“Child”,Child.getId());
添加(dbref);
});
子文档put(键、数据库列表);
});
文件.付诸表决(“儿童”,子文件);
}
归还文件;
}
}
还可以通过添加自定义转换来更新mongo的配置:
@Configuration
@EnableConfigurationProperties(MongoProperties.class)
@EnableMongoRepositories(basePackages = "com.my.repository")
public class MongoConfig extends AbstractMongoConfiguration {
private MongoProperties properties;
public MongoConfig(MongoProperties properties) {
this.properties = properties;
}
@Override
public MongoClient mongoClient() {
return new MongoClient(properties.getHost());
}
@Override
protected String getDatabaseName() {
return properties.getDatabase();
}
@Bean
@Override
public CustomConversions customConversions() {
List<Converter<?, ?>> converterList = new ArrayList<>();
converterList.add(new ChildWriteConverter());
return new MongoCustomConversions(converterList);
}
}
@配置
@EnableConfigurationProperties(MongoProperties.class)
@EnableMongorPositories(basePackages=“com.my.repository”)
公共类MongoConfig扩展了AbstractMongoConfiguration{
私人物业;
公共MongoConfig(MongoProperties属性){
这个。属性=属性;
}
@凌驾
公共MongoClient MongoClient(){
返回新的MongoClient(properties.getHost());
}
@凌驾
受保护的字符串getDatabaseName(){
返回properties.getDatabase();
}
@豆子
@凌驾
公共CustomConversions CustomConversions(){
列表我不知道它是否实际存在,但目前我使用Spring Converter对此进行了解决。如果您的对象不太复杂,您可以执行相同的操作或改进,使其更通用:
@WritingConverter
public class ChildWriteConverter implements Converter<Child, Document> {
@Override
public Document convert(Child source) {
//TODO: Think about generic solution
Document document = new Document();
document.put("_id", source.getId());
Map<String, List<Child>> children = source.getChildren();
Document subdocument = new Document();
if(children != null) {
children.forEach((key, childList) -> {
List<DBRef> dbList = new ArrayList<>(childList.size());
childList.forEach(child -> {
DBRef dbref = new DBRef("Child", child.getId());
dbList.add(dbref);
});
subdocument.put(key, dbList);
});
document.put("children", subdocument);
}
return document;
}
}
@WritingConverter
公共类ChildWriteConverter实现转换器{
@凌驾
公共文档转换(子源){
//TODO:考虑通用解决方案
文档=新文档();
document.put(“_id”,source.getId());
Map children=source.getChildren();
文档子文档=新文档();
如果(子项!=null){
children.forEach((键,childList)->{
List dbList=newarraylist(childList.size());
childList.forEach(child->{
DBRef DBRef=newdbref(“Child”,Child.getId());
添加(dbref);
});
子文档put(键、数据库列表);
});
文件.付诸表决(“儿童”,子文件);
}
归还文件;
}
}
还可以通过添加自定义转换来更新mongo的配置:
@Configuration
@EnableConfigurationProperties(MongoProperties.class)
@EnableMongoRepositories(basePackages = "com.my.repository")
public class MongoConfig extends AbstractMongoConfiguration {
private MongoProperties properties;
public MongoConfig(MongoProperties properties) {
this.properties = properties;
}
@Override
public MongoClient mongoClient() {
return new MongoClient(properties.getHost());
}
@Override
protected String getDatabaseName() {
return properties.getDatabase();
}
@Bean
@Override
public CustomConversions customConversions() {
List<Converter<?, ?>> converterList = new ArrayList<>();
converterList.add(new ChildWriteConverter());
return new MongoCustomConversions(converterList);
}
}
@配置
@EnableConfigurationProperties(MongoProperties.class)
@EnableMongorPositories(basePackages=“com.my.repository”)
公共类MongoConfig扩展了AbstractMongoConfiguration{
私人物业;
公共MongoConfig(MongoProperties属性){
这个。属性=属性;
}
@凌驾
公共MongoClient MongoClient(){
返回新的MongoClient(properties.getHost());
}
@凌驾
受保护的字符串getDatabaseName(){
返回properties.getDatabase();
}
@豆子
@凌驾
公共CustomConversions CustomConversions(){
列表