我试图使用Spring Boot在MongoDB中创建数据库。我想创建两个集合,一个DbConnections集合和一个Configuration集合。我想做的是DbConnections集合中的每个连接都有其自己的Configurations,就像使用外键的SQL引用一样。我试图使用@DBRef
注释执行此操作,如下所示:
@Document(collection = "connections")
public class DbConnection {
@Transient
public static final String SEQUENCE_NAME = "connection_sequence";
@Id
private long id;
private String username;
private String password;
private String connectionUrl;
private long fkIdUsuario;
@DBRef(db = "configurations")
private List<Configuration> configurations;
public DbConnection() {
}
public DbConnection(String username, String password, String connectionUrl) {
this.username = username;
this.password = password;
this.connectionUrl = connectionUrl;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public List<Configuration> getConfigurations() {
return configurations;
}
public void setConfigurations(List<Configuration> configurations) {
this.configurations = configurations;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConnectionUrl() {
return connectionUrl;
}
public void setConnectionUrl(String connectionUrl) {
this.connectionUrl = connectionUrl;
}
public long getFkIdUsuario() {
return fkIdUsuario;
}
public void setFkIdUsuario(long fkIdUsuario) {
this.fkIdUsuario = fkIdUsuario;
}
}
和Configuration类:
@Document(collection = "configurations")
public class Configuration {
@Transient
public static final String SEQUENCE_NAME = "config_sequence";
@Id
private long id;
private int fkIdConnection;
private String name;
private String query;
private BasicDBObject values;
private BasicDBObject result;
public Configuration() {
}
public Configuration(long id, int fkIdConnection, String name, String query, BasicDBObject values, BasicDBObject result) {
this.id = id;
this.fkIdConnection = fkIdConnection;
this.name = name;
this.query = query;
this.values = values;
this.result = result;
}
public int getFkIdConnection() {
return fkIdConnection;
}
public void setFkIdConnection(int fkIdConnection) {
this.fkIdConnection = fkIdConnection;
}
public Map getValues(){
if(null != values){
return values.toMap();
}
return null;
}
public void setValues(){
this.values = new BasicDBObject(values);
}
public Map getResult(){
if(null != result){
return result.toMap();
}
return null;
}
public void setResult(){
this.result = new BasicDBObject(result);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
}
我将这样的数据保存在控制器中:
对于DbConnectionsController:
@PostMapping(value = "/conexion")
public DbConnection addConnection(@RequestBody DbConnectionModel dbConnectionModel){
DbConnection dbConnection = new DbConnection(dbConnectionModel.getUsername(), dbConnectionModel.getPassword(), dbConnectionModel.getConnectionUrl());
dbConnection.setFkIdUsuario(getIdUsuario());
return dbConnectionRepository.save(dbConnection);
}
以及配置控制器的保存方法:
@PostMapping
public Configuration agregarConfiguracion(@RequestBody Configuration configuration){
configuration.setId(sequenceGeneratorService.generateSequence(Configuration.SEQUENCE_NAME));
return configurationRepository.save(configuration);
}
现在,当我保存一个
Connection
时,它在mongo对象中没有字段configurations
,并且当我创建一个新的Configuration
时,它仍然没有显示在DbConnection
中。 DbConnections对象发出GET
请求,但configurations
的DbConnections
字段显示为空。为什么会发生这种情况,我该如何解决? 最佳答案
DBRef返回空数组时,我遇到了类似的问题。几个有助于我诊断的步骤:
1)您的2个收藏集connections
和configurations
是否保存在2个单独的数据库中?如果不是,则需要在(db = "configurations")
之后删除@DBRef
。
@DBRef
private List<Configuration> configurations;
2)直接连接到数据库时,您能够找到对象吗?我使用Robot 3T(一种GUI工具)直接查看Mongo DB,以检查所保存数据的形状。确保正确设置
connections
,并且还应为每个configurations
看到connection
,如下所示:configurations: [
{
"$ref": "configurations",
"$id": ObjectId("asdfadsgasdfasdfa")
}
]
3)您可以单步进入类org.springframework.data.mongodb.core.convert.DefaultDbRefResolver方法fetch()。 (我正在使用具有相同版本的spring-boot-starter-data-mongodb依赖项的springboot 2.1.6.RELEASE)
@Override
public Document fetch(DBRef dbRef) {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Fetching DBRef '{}' from {}.{}.", dbRef.getId(),
StringUtils.hasText(dbRef.getDatabaseName()) ? dbRef.getDatabaseName() : mongoDbFactory.getDb().getName(),
dbRef.getCollectionName());
}
StringUtils.hasText(dbRef.getDatabaseName());
return getCollection(dbRef).find(Filters.eq("_id", dbRef.getId())).first();
}
确保
dbRef
的所有属性(例如,collectionName,db,id)都是正确的,否则它将无法找到相关的对象。关于java - MongoDB DBRef列表在Spring Boot中返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57021914/