Announcement Announcement Module
Collapse
No announcement yet.
Composite Key for Mongo _id field is not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Composite Key for Mongo _id field is not working

    I am using Mongo Spring Data.
    I wanted to make _id field of mongodb as composite key. I have written
    a Java class (UniqueId) and declared the id field of UniqueId type.
    Here is the code that is am using

    @Document
    public class Person {

    @Id
    private UniqueId id;

    private String name;

    private int age;
    }


    public class UniqueId implements Serializable {
    private String upid;
    private String tenanatId;


    public UniqueId(String upid, String tenanatId) {
    super();
    this.upid = upid;
    this.tenanatId = tenanatId;
    }

    When i am saving the document, it is giving exception "UniqueId cann't
    be serialized
    " even if it implements Serializable .Please help.

    Here is the full stack trace
    Exception in thread "main" java.lang.IllegalArgumentException: can't
    serialize class com.test.domain.UniqueId
    at org.bson.BSONEncoder._putObjectField(BSONEncoder.j ava:234)
    at org.bson.BSONEncoder.putObject(BSONEncoder.java:12 1)
    at org.bson.BSONEncoder.putObject(BSONEncoder.java:86 )
    at com.mongodb.OutMessage.putObject(OutMessage.java:1 90)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLa yer.java:253)
    at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLa yer.java:217)
    at com.mongodb.DBCollection.insert(DBCollection.java: 87)
    at org.springframework.data.mongodb.core.MongoTemplat e
    $10.doInCollection(MongoTemplate.java:669)
    at
    org.springframework.data.mongodb.core.MongoTemplat e.execute(MongoTemplate.java:
    329)
    at
    org.springframework.data.mongodb.core.MongoTemplat e.insertDBObject(MongoTemplate.java:
    665)
    at
    org.springframework.data.mongodb.core.MongoTemplat e.doInsert(MongoTemplate.java:
    571)
    at
    org.springframework.data.mongodb.core.MongoTemplat e.insert(MongoTemplate.java:
    530)
    at
    org.springframework.data.mongodb.core.MongoTemplat e.insert(MongoTemplate.java:
    521)
    at com.test.HelloMongo.run(HelloMongo.java:34)
    at com.test.HelloMongo$$FastClassByCGLIB$
    $f4b08fd1.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy. java:191)
    at org.springframework.aop.framework.Cglib2AopProxy
    $CglibMethodInvocation.invokeJoinpoint(Cglib2AopPr oxy.java:688)
    at
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :
    150)
    at
    org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor.invoke(PersistenceExcepti onTranslationInterceptor.java:
    155)
    at
    org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :
    172)
    at org.springframework.aop.framework.Cglib2AopProxy
    $DynamicAdvisedInterceptor.intercept(Cglib2AopProx y.java:621)
    at com.test.HelloMongo$$EnhancerByCGLIB$$76fa74ae.run (<generated>)

  • #2
    Please reply if anyone is having any idea

    Comment

    Working...
    X