Hi,
Related to this question is the response in the your post here.
If you use a type other than String, BigInteger or ObjectId for the id field, then there is no automatic generation of an Id value. The link to the docs is in the other post. What was stored in your case was a document like this
Code:
{ "_id" : 0, "_class" : "org.springframework.data.mongodb.core.PostThread", "categoryId" : 2 }
The _id field is set to 0 and is an integer. Issue https://jira.springsource.org/browse/DATAMONGO-322 will enforce these rules around populating the id field on save.
FWIW,
Using your definition for PostThread and the following code
Code:
PostThread pt = new PostThread();
pt.setCategoryId(2);
template.save(pt);
System.out.println(thread);
and
Code:
DBObject dbo = template.execute(PostThread.class, new CollectionCallback<DBObject>()
{
public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException {
BasicDBObject dbo = new BasicDBObject();
dbo.put("categoryId", 2);
return collection.findOne(dbo);
} });
System.out.println(dbo.get("_id").getClass().toString());
gives -
PostThread [id=0, categoryId=2]
class java.lang.Integer
Cheers,
Mark