Announcement Announcement Module
Collapse
No announcement yet.
Spring data can not serialize entity on find and remove execution Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring data can not serialize entity on find and remove execution

    I'm using spring-data-mongodb-1.2.0.RELEASE. I have two classes A and B where B has a reference to A and it is annotated with @DBRef.

    Class A:

    Code:
    @Document(collection = "a")
    public class A {
    @Id
    public String id;
    
    /** The TicketGrantingTicket this is associated with. */
    @Field
    public String name;
    
    public A(String id, String name) {
        this.id = id;
        this.name = name;
    }
    }
    Class B:

    Code:
    @Document(collection = "b")
    public class B {
    
    @Id
    public String id;
    
    @Field
    public String name;
    
    @DBRef
    @Indexed
    public A a;
    
    public B(String id, String name, A a) {
        super();
        this.id = id;
        this.name = name;
        this.a = a;
    }
    }
    After creating and associating A and B instances:

    Code:
    A a1 = new A("a-2", "a-2");
    mongoOperations.insert(a1);
    
    B b1 = new B("b-2", "b-2", a1);
    mongoOperations.insert(b1);
    I'm able to execute the next query:

    Code:
    B fromDB = mongoOperations.findOne(Query.query(Criteria.where("a").is(a1)), B.class);
    But(!!!), when I'm executing findAndRemove:

    Code:
    fromDB = mongoOperations.findAndRemove(Query.query(Criteria.where("a").is(a1)), B.class);
    I'm getting this:

    java.lang.RuntimeException: json can't serialize type : class org.jasig.cas.ticket.registry.entities.A at com.mongodb.util.ClassMapBasedObjectSerializer.ser ialize(ClassMapBasedObjectSerializer.java:77) at com.mongodb.util.JSONSerializers$MapSerializer.ser ialize(JSONSerializers.java:307) at com.mongodb.util.ClassMapBasedObjectSerializer.ser ialize(ClassMapBasedObjectSerializer.java:79) at com.mongodb.util.JSON.serialize(JSON.java:56) at com.mongodb.util.JSON.serialize(JSON.java:41) at com.mongodb.BasicDBObject.toString(BasicDBObject.j ava:83) at java.lang.String.valueOf(String.java:2826) at java.lang.StringBuilder.append(StringBuilder.java: 115) at org.springframework.data.mongodb.core.MongoTemplat e.doFindAndRemove(MongoTemplate.java:1392) at org.springframework.data.mongodb.core.MongoTemplat e.findAndRemove(MongoTemplate.java:572) at org.springframework.data.mongodb.core.MongoTemplat e.findAndRemove(MongoTemplate.java:568)

    Does someone have an idea why?

  • #2
    This is a "bug" as we use XSD ids for the id attribute which enforces uniqueness in one XML file. I've created DATAMONGO-648 [0] for you. In the meantime you should get this working by having the two declarations in separate config files. Alternatively have a look at the JavaConfig options.

    [0] https://jira.springsource.org/browse/DATAMONGO-648

    Comment


    • #3
      Originally posted by Oliver Gierke View Post
      This is a "bug" as we use XSD ids for the id attribute which enforces uniqueness in one XML file. I've created DATAMONGO-648 [0] for you. In the meantime you should get this working by having the two declarations in separate config files. Alternatively have a look at the JavaConfig options.

      [0] https://jira.springsource.org/browse/DATAMONGO-648
      Hi Oliver,
      Sorry, but I don't understand what do you mean by separating the declarations to two config files? declarations of what?

      Comment

      Working...
      X