java - Spring-data-mongodb 不会在列表中保留多个对象

标签 java spring mongodb spring-mvc

我正在使用 Spring-data-mongodb,我可以将一个对象保留在列表中,但是当我尝试添加另一个对象时,它不起作用,应用程序不会抛出异常。

这是我的 Json:

[
  {
    idUser: "4a9f10d9-e19f-42af-ba00-891a567cc41f",
    login: "peter",
    password: "mypassword",
    email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9c9dccddccbf9dcdcdcdc97dad6d4" rel="noreferrer noopener nofollow">[email protected]</a>",
    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 元素是正确的,第二个元素插入为 null。

我留下我的代码:

用户.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;
}

病人.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

我需要帮助。 非常感谢

最佳答案

首先,如果您将 Spring Data 与 MongoDB 一起使用,请正确使用它:

@Repository
public interface UserRepository extends MongoRepository<User, String> {

}

现在只需通过 @Autowired 注入(inject) 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<>();

关于java - Spring-data-mongodb 不会在列表中保留多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31942621/

相关文章:

java - 计算鼠标悬停时的工具提示而不是加载表格

java - 使用 JndiObjectFactoryBean 在运行时设置动态模式

java - 以某个对象开始的子列表

java - 日期格式化后以小写形式显示 AM 和 PM

c#-4.0 - 如何判断记录是否存在于 Mongo 集合中 (C#)

java - 没有定义命名的 bean

java - 将 CDI/EJB 注解迁移到 Spring 注解

java - Tomcat,java.lang.ClassNotFoundException : org. springframework.web.servlet.DispatcherServlet

django - Djongo 重新定义 ArrayModelField 的结构

c# - 在 LINQ lambda 表达式中使用字符串变量