hello
I have 2 pojos (in java) , each one has an Id as objectId, and a field marked as @reference with a collection of the other class.
to be more clear, say the two pojos are users of computers and computers; an user can open a session on many computers and a computer can have sessions of many users opened.
when I try to create an user, with its fields, and a computer, and I link them via their ArrayList fields, I have this error :
@Id field cannot be null.
here is some code, a little simplier than what I explained:
pojo1 : a car
**********************************************************************
@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;
}
}**********************************************
pojo2 : a driver
**********************************************
@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 + "]";
}
}
**********************************************
the main class:
**********************************************
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);
could you tell me if I have to generate an Id for the referenced class; and could you explain me how to solve this dilemna : class A contains a reference on B, B contains a reference on A, I have to save one of the two classes before the other and then the other class has no Id generated (it's by this way I understood the error message).
thanks,
olivier SAINT-EVE