我正在使用Spring-data-mongodb,我可以在列表中保留一个对象,但是当我尝试添加另一个时,它不起作用,应用程序不会抛出异常。
这是我的Json:
[
{
idUser: "4a9f10d9-e19f-42af-ba00-891a567cc41f",
login: "peter",
password: "mypassword",
email: "peter@eeee.com",
patients:
[
{
idPatient: "d31e8052-36d3-4285-9f97-454f3437812d",
name: "ada",
birthday: 1363474800000,
idUser: "4a9f10d9-e19f-42af-ba00-891a567cc41f",
region:
{
idRegion: "d8acfa45-486e-49e0-b4e6-edde6743cf30",
name: "Madrid"
},
personalCalendars: null
},
null
]
}
]
如你所见,我的第一个Patient元素是正确的,第二个是insert为null。
我留下我的代码:
User.java
@Document(collection = "users")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String id;
@Indexed
private UUID idUser;
@Indexed(unique = true)
private String login;
private String password;
@Indexed(unique = true)
private String email;
@DBRef
private List<Patient> patients;
@PersistenceConstructor
public User(UUID idUser, String login, String password, String email, List<Patient> patients){
this.idUser = idUser;
this.login = login;
this.password = password;
this.email = email;
this.patients = patients;
}
Patient.java
@Document(collection = "patients")
public class Patient implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String id;
@Indexed
private UUID idPatient;
private String name;
private Date birthday;
private UUID idUser;
private Region region;
@Transient
private List<PersonalCalendar> personalCalendars;
@PersistenceConstructor
public Patient(UUID idPatient, String name, Date birthday,UUID idUser, Region region){
this.idPatient = idPatient;
this.name = name;
this.birthday = birthday;
this.idUser = idUser;
this.region = region;
}
和我做插入的DAO。
@Override
public Patient createPatient(User user, Patient patient) {
this.mongoOps.save(patient , "patients");
this.mongoOps.save(user , "users");
return this.getPatientById(patient.getIdPatient());
}
控制台会返回此信息,但患者不会:
15:16:16.718 [tomcat-http--6] DEBUG o.s.data.mongodb.core.MongoTemplate - Saving DBObject containing fields: [_class, _id, idPatient, name, birthday, idUser, region]
15:16:16.723 [tomcat-http--6] DEBUG o.s.data.mongodb.core.MongoDbUtils - Getting Mongo Database name=[application]
15:16:16.747 [tomcat-http--6] DEBUG org.mongodb.driver.protocol.insert - Inserting 1 documents into namespace application.patients on connection [connectionId{localValue:2, serverValue:119}] to server 127.0.0.1:27017
15:16:16.761 [tomcat-http--6] DEBUG org.mongodb.driver.protocol.insert - Insert completed
我需要帮助。 非常感谢
答案 0 :(得分:3)
首先,如果您将Spring Data与MongoDB一起使用,请正确使用它:
@Repository
public interface UserRepository extends MongoRepository<User, String> {
}
现在只需通过@Autowired
注释注入UserRepository:
@Autowired
private UserRepository userRepository;
User user = new User();
Patient patient = new Patient();
user.addPatient(patient);
// Just call save from userRepository to save your User with Patient.
// save method will return instance of saved user (together with instance of
// patient)
User user = userRepository.save(user);
请注意,save
方法也可用于更新现有User
。如果User
是新的(没有生成id),它将被插入。如果用户存在(已生成id),则只会更新。
假设User
类有addPatient
方法,如下所示:
public void addPatient(Patient patient) {
this.patients.add(patient);
}
另外,请确保您的列表已初始化:List<Patient> patients = new ArrayList<>();