@reference and error : @Id field cannot be nul

346 views
Skip to first unread message

lolveley

unread,
Jul 29, 2012, 2:41:47 PM7/29/12
to mor...@googlegroups.com
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

Scott Hernandez

unread,
Jul 29, 2012, 2:45:58 PM7/29/12
to mor...@googlegroups.com
Just change your entities to declare the id field with an initial value:

@Id ObjectId id = new ObjectId();

The id will be assigned during your save, but not by the server. So
either way the client will have to create the ObjectId.

lolveley

unread,
Jul 30, 2012, 3:21:36 AM7/30/12
to mor...@googlegroups.com
hi scott

I understood your answer but I have a question : if I create the objectId at the same time as the creation of the class, how the computer can set a objectId different than the objectId already stored in the base? I mean are the objectId differents one from another? how is it made?

Scott Hernandez

unread,
Jul 30, 2012, 7:33:03 AM7/30/12
to mor...@googlegroups.com
I'm not sure I understand. Here is a description of what an ObjectId
is: http://www.mongodb.org/display/DOCS/Object+IDs

They are like UUIDs and you can create many at the same time and they
will all be different.

lolveley

unread,
Jul 31, 2012, 2:49:31 PM7/31/12
to mor...@googlegroups.com
thank you, it worked.
I understood. objectId are like ISBN for books : an unique identifier, and it's not generated like in RDMS, I mean the new objectId does not depand of those which are already stored.

thanks

olivier

Reply all
Reply to author
Forward
0 new messages