spring - Spring Data Firestore 中的外键/DBRef
问题描述
我有 2 个实体: 1- 用户:
@Document(collectionName = CollectionConstants.USER_COLLECTION)
public class User {
@DocumentId
protected String id;
private String username;
}
2- 比赛:
@Document(collectionName = CollectionConstants.CONTEST_COLLECTION)
public class Contest {
private List<User> contestants;
}
如何在让 Spring Data 自动管理的同时仅将用户的 ID(无论是用户列表还是单个用户)保存在数据库中?
我实际上正在寻找以下替代方案:
春季数据 JPA
@OneToMany
:,@ManyToOne
弹簧数据 MongoDB:
@DBRef
解决方案
我已经检查了Spring Cloud GCP for Firestore,它指出:
starter 在 Spring 应用程序上下文中自动配置和注册 Firestore bean。要开始使用它,只需使用
@Autowired
注释。
@Autowired
Firestore firestore;
void writeDocumentFromObject() throws ExecutionException, InterruptedException {
// Add document data with id "joe" using a custom User class
User data = new User("Joe",
Arrays.asList(
new Phone(12345, PhoneType.CELL),
new Phone(54321, PhoneType.WORK)));
// .get() blocks on response
WriteResult writeResult = this.firestore.document("users/joe").set(data).get();
LOGGER.info("Update time: " + writeResult.getUpdateTime());
}
User readDocumentToObject() throws ExecutionException, InterruptedException {
ApiFuture<DocumentSnapshot> documentFuture =
this.firestore.document("users/joe").get();
User user = documentFuture.get().toObject(User.class);
LOGGER.info("read: " + user);
return user;
}
有示例https://github.com/spring-cloud-gcp/spring-cloud-gcp-samples/spring-cloud-gcp-firestore-sample
推荐阅读
- windows - 为 windows 编译 openexr 时出现 Visual Studio 和 cmake 错误
- node.js - 如何使用 Webpack 为 Rest API Express NodeJS 服务器创建包
- php - PDO 为多个条件返回空
- php - PHP substr 返回少于预期
- delphi - LockBox 3 加密不匹配在线工具,怀疑填充或密钥问题
- windows - BCDEdit 错误,参数不正确
- javascript - Vue将对象复制到本地数据
- c# - Mono.Data.Sqlite 可以与 Xamarin 一起使用吗
- html - 溢出:自动和固定高度使 Microsoft Edge 中的下拉文本模糊
- angular - rxjs 处理错误与拦截器不兼容