【发布时间】:2019-01-27 07:56:44
【问题描述】:
我正在使用 spring data(spring boot) 和 mongodb。我有这两个 实体
@Document(collection = "users")
public class UserEntity {
private String email;
@DBRef
private DeviceEntity device;
}
@Document(collection = "device")
public class DeviceEntity {
private String name;
}
and I am creating the first object of device then setting it to user
entity. Now i will save user entity.
DeviceEntity Device = new DeviceEntity();
device.setName("demo");
UserEntity user = new UserEntity();
user.setEmail("demo@gmail.com");
user.setDevice( device );
userRepo.save( user );
然后我得到了这个错误:
“无法创建对具有 NULL id 的对象的引用。] 与 root 导致 org.springframework.data.mapping.model.MappingException: 不能 创建对具有 NULL id mongo hibernate 的对象的引用。”
谁能解释我们如何使用设备实体存储用户实体。如果我先保存设备实体并设置为用户实体,我可以正常工作,但我只想保存用户实体,它会自动保存设备实体。
【问题讨论】:
标签: mongodb spring-boot