Announcement Announcement Module
No announcement yet.
Ignore existing id field custom-converters not working on save/insert Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Ignore existing id field custom-converters not working on save/insert

    I am attempting to migrate my project from using a traditional JPA/SQL model (relational) to using a SpringData/Document model. We have many @Entity pojos that I wanted to migrate in parts to mongo. The issue is all of these classes inherit from a base class with a Long id field which is a no no in mongo. The error I get when I attempt to save these pojo's directly with out modification is:

    org.springframework.dao.InvalidDataAccessApiUsageException: Cannot autogenerate id of type java.lang.Long for entity of type!
    	at com.thumbsupapp.engine.service.tms.impl.TmsDataProcessorImpl.processTmsDiscoveryPrograms(
    This makes sense and I understand the issues with using Long as an id in mongo. I tried to avoid this issue by putting annotation on another field but it seams that this is ignored. Throwing the same error as above.

    public class TvShow extends AbstractBaseEntity<TvShow> {
    	private static final long serialVersionUID = -566057405515037201L;
            private BigInteger mongoId;
    	private String title;
    	private String shortTitle;
    Next I tried to use a custom converter which I registered in my context.xml like this:

    <bean id="tvShowReaderConverter" class="com.thumbsupapp.engine.document.converter.TvShowReaderConverter"/>
    	<bean id="tvShowWriterConverter" class="com.thumbsupapp.engine.document.converter.TvShowWriterConverter"/>
    <mongo:db-factory id="mongoDbFactory" dbname="thumbsup_metrics" mongo-ref="mongo"/>
    	<mongo:mapping-converter base-package="com.thumbsup.entity">
        		<mongo:converter ref="tvShowReaderConverter"/>
        		<mongo:converter ref="tvShowWriterConverter"/>
    	<bean id="mongoTemplate" class="">
      		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
      		<constructor-arg name="mongoConverter" ref="mappingConverter"/>
    Now when I attempt to execute the following code I get the same InvalidDataAccessApiUsageException: Cannot autogenerate id of type java.lang.Long exception.


    This leads me to believe that the standard spring conversion is taking place and attempting to set my id field and its not delegating to my custom converter. I can prove my converter is in place and working by executing the following which produces a nice DBObject:

    MongoConverter converter = mongoTemplate.getConverter();
    DBObject sink = new BasicDBObject();
    converter.write(show, sink);
    LOG.debug("Converted object to DBObject " + sink.get("uuid"));
    With all that said I have 3 questions.

    1. Dose a method exists to ignore an existing id field in an object in favor of another when using mongoTemplate.

    2. Do any issues exist with my approach to using custom converters and can I force a converter to be the only one that fires for this type of object.

    3. Am I simply barking up the wrong tree here and is what I am attempting possible.