gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:03:32 24 4
gpt4 key购买 nike

我正在使用 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 元素是正确的,第二个元素插入为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com