如何在mongodb spring-data中使用DBRef保存文档

时间:2019-04-26 11:53:09

标签: java spring-data spring-mongodb

我有扩展MongoRepository的Spring数据存储库类。因此,我正在执行POST请求以创建一条记录,其中包含多个引用字段以引用其他文档。

存储库界面:

@RepositoryRestResource(collectionResourceRel = "user",path = "user")
 public interface UserRepository extends MongoRepository<User,String>{
}

Model类:

@Document(collection = "user")
public class User {
@Id
private String id;
private String email;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Date createdTime;
@Field("infoSignups")
@DBRef
private InfoSignUp infoSignups;
@Field("webinarSignups")
@DBRef
private WebinarSignUp webinarSignups;

}

@Document(collection = "infoSignups")
public class InfoSignUp {

@Id
private String id;
private String email;

}

@Document(collection = "webinarSignups")
public class WebinarSignups {

@Id
private String id;
private String email;

}

POST请求:

{  
  "email":"sheen.example@gmail.com",
  "name":"test",
  "infoSignups":[  
    {  
     "id":"5cbeb33199a4640b94ba1de8"
     }
   ],
  "webinarSignups":[  
    {  
     "id":"5cc0136599a4641d835d3259"
    }
   ],
  "businessName":"test data",
  "phone":"test data",
  "address":"test data",
  "createdTime":"2015-09-26T01:30:00.000Z"
 }

但是当我对存储库端点进行POST请求时,未保存参考字段,其他字段保存在集合中。我是否必须在不使用Spring数据的情况下手动持久化对象?如何使用对端点的POST请求保存对其他集合的引用?

0 个答案:

没有答案