【问题标题】:Spring-data-mongodb not persist multiple objects on listSpring-data-mongodb 不在列表中保留多个对象
【发布时间】:2015-11-03 17:29:35
【问题描述】:

我正在使用 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 as 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;
}

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

我需要帮助。 非常感谢

【问题讨论】:

  • 什么是例外?
  • 也不例外,我会用控制台回答更新
  • 我只能在集合中添加一个对象,我不知道为什么

标签: java spring mongodb spring-mvc


【解决方案1】:

首先,如果你在MongoDB中使用Spring Data,请正确使用:

@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&lt;Patient&gt; patients = new ArrayList&lt;&gt;();

【讨论】:

  • 这是我的错误,我使用的是 Indexed(unique),并且在进行更改时没有删除集合。由于这个原因,mongodb 不适合它。但是感谢存储库的建议,我更新了我的项目。
  • @JavierOliver 欢迎您。总是给自己一些时间。 ;)
猜你喜欢
  • 1970-01-01
  • 2016-10-26
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
  • 1970-01-01
相关资源
最近更新 更多