Results 1 to 2 of 2

Thread: int id saved as string

  1. #1
    Join Date
    Nov 2011
    Posts
    5

    Default int id saved as string

    Code:
    @Document
    public class PostThread {
    
    	@Id
    	private int id;
    }
    int id saved as string.
    how to save int as int ?

  2. #2
    Join Date
    Aug 2004
    Location
    New York, NY
    Posts
    74

    Default

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •