2
entité compte
comment obtenir inséré id objet dans jongo avec MongoDB
public class Account
{
@MongoObjectId
private String _id;
private String name;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class JongoExample {<br>
MongoClient mongoClient = new MongoClient(); // code to connect with database
DB db = mongoClient.getDB("kommboxj");
Jongo jongo = new Jongo(db);<br>
MongoCollection accountCollection = jongo.getCollection("account");
Account account = new Account();<br>
account.setName("rks");<br>
String accountIdMongo = (String) accountCollection.insert(account).getUpsertedId();}
Ici, je trouve acountIdMongo est nul, ne savent pas comment l'obtenir, ici, je veux compte ObjectId inséré.
Solution obtenue. Après l'utilisation de l'annotation @MongoObjectId dans l'entité, son auto-mappage, maintenant je suis en mesure d'obtenir par account.getId() après la méthode insert. Pas besoin d'upsertedId –
Veuillez mettre votre solution en réponse à la question. – henrikstroem