bonjour
je teste actuellement mongodb + morphia.
j'ai un problème avec des références dans mes POJOs :
programme principal:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Morphia morphia = new Morphia();
morphia.map(Voiture.class).map(Conducteur.class);
Datastore ds = morphia.createDatastore("Voitures_conducteurs");
Conducteur jean = new Conducteur();
jean.setNom("jean");
jean.setBonus(25);
Voiture porsche=new Voiture("211 XB 57");
jean.addVoiture(porsche);
porsche.setConducteur(jean);
ds.save(jean); |
erreur :
@Id field cannot be null!
voici mes pojos:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| @Entity
public class Voiture {
@Id ObjectId id;
String immatriculation;
@Reference
Conducteur conducteur;
public String getImmatriculation() {
return immatriculation;
}
public void setImmatriculation(String immatriculation) {
this.immatriculation = immatriculation;
}
public Conducteur getConducteur() {
return conducteur;
}
public void setConducteur(Conducteur conducteur) {
this.conducteur = conducteur;
}
public Voiture(String immatriculation) {
super();
this.immatriculation = immatriculation;
}
@Override
public String toString() {
return "Voiture [immatriculation=" + immatriculation + "]";
}
public ObjectId getId() {
return id;
}
public void setId(ObjectId id) {
this.id = id;
}
} |
et
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| @Entity
public class Conducteur {
@Id ObjectId id;
String nom;
Integer bonus;
@Reference
ArrayList<Voiture> voitures;
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public Integer getBonus() {
return bonus;
}
public void setBonus(Integer bonus) {
this.bonus = bonus;
}
public ArrayList<Voiture> getVoitures() {
return voitures;
}
public void setVoitures(ArrayList<Voiture> voitures) {
this.voitures = voitures;
}
public void addVoiture(Voiture to_add){
voitures.add(to_add);
}
public Conducteur() {
super();
voitures=new ArrayList<Voiture>();
}
@Override
public String toString() {
return "Conducteur [nom=" + nom + "]";
}
} |
merci
olivier
Partager