2

I am using Spring-data-mongodb and i can persist an object on a list, but when i try to add another, it doesn't work, the application doesn't throw an exception.

this is my Json:

[
  {
    idUser: "4a9f10d9-e19f-42af-ba00-891a567cc41f",
    login: "peter",
    password: "mypassword",
    email: "[email protected]",
    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
      ]
   }
]

As you can see, my first Patient element is correctly, and the second was insert as null.

I leave my code:

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;
}

and the DAO whereI do the insert.

@Override
public Patient createPatient(User user, Patient patient) {
    this.mongoOps.save(patient , "patients");
    this.mongoOps.save(user , "users");
    return this.getPatientById(patient.getIdPatient());
}

The console returns this, but no persists the patient:

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

I need help. Thanks a lot

3
  • And what is an exception? Commented Aug 11, 2015 at 13:11
  • no exception, I will update with console answer Commented Aug 11, 2015 at 13:17
  • I only can add a object in the collection and I don't know why Commented Aug 11, 2015 at 14:28

1 Answer 1

3

First, if you use Spring Data with MongoDB, use it properly:

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

}

Now just inject UserRepository via @Autowired annotation:

@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);

Note that save method can also be used for updating of existing User. If User is new (not having generated id) it will be inserted. If user exists (has generated id) it will be just updated.

Presuming that User class has a addPatient method that looks like this:

public void addPatient(Patient patient) {
    this.patients.add(patient);
}

Also, make sure that your list is initialized: List<Patient> patients = new ArrayList<>();

Sign up to request clarification or add additional context in comments.

2 Comments

It was my mistake, i was using a Indexed(unique) and I didn't drop the collection when I did my changes. The mongodb wasn't work for it for this reason. However thanks for the repository suggestion, I updated my project.
@JavierOliver You are welcome. Always give yourself some time. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.