这就是我想要做的事情,它无法正常工作。
@DBRef( db = "employee")
private Map<Role, List<Employee>> rolesByGroup;
答案 0 :(得分:2)
你应该这样做:
public class EmployeeList {
@DBRef public List<Employee> employees = new ArrayList<>();
}
private Map<Role, EmployeeList> rolesByGroup;
答案 1 :(得分:0)
我不知道这是不是真的,但是目前我使用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;
}
}
还通过添加自定义转换来更新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);
}
}
您还可以找到与此票有关的票证:https://jira.spring.io/browse/DATAMONGO-2132