Announcement Announcement Module
Collapse
No announcement yet.
Ignore existing id field custom-converters not working on save/insert Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    org.springframework.dao.InvalidDataAccessApiUsageException: Cannot autogenerate id of type java.lang.Long for entity of type com.thumbsupapp.core.entity.tv.TvShow!
    	at org.springframework.data.mongodb.core.MongoTemplate.assertUpdateableIdIfNotSet(MongoTemplate.java:917)
    	at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:713)
    	at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:708)
    	at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:704)
    	at com.thumbsupapp.engine.service.tms.impl.TmsDataProcessorImpl.processTmsDiscoveryPrograms(TmsDataProcessorImpl.java:91)
    This makes sense and I understand the issues with using Long as an id in mongo. I tried to avoid this issue by putting @org.springframework.data.annotation.Id annotation on another field but it seams that this is ignored. Throwing the same error as above.

    Code:
    public class TvShow extends AbstractBaseEntity<TvShow> {
    
    	private static final long serialVersionUID = -566057405515037201L;
            
            @org.springframework.data.annotation.Id
            private BigInteger mongoId;
    
    	@Column(name="TITLE")
    	private String title;
    	
    	@Column(name="SHORT_TITLE")
    	private String shortTitle;
    ....
    Next I tried to use a custom converter which I registered in my context.xml like this:

    Code:
    <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:custom-converters>
        		<mongo:converter ref="tvShowReaderConverter"/>
        		<mongo:converter ref="tvShowWriterConverter"/>
      		</mongo:custom-converters>
    	</mongo:mapping-converter>
    	
    	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
      		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
      		<constructor-arg name="mongoConverter" ref="mappingConverter"/>
    	</bean>
    Now when I attempt to execute the following code I get the same InvalidDataAccessApiUsageException: Cannot autogenerate id of type java.lang.Long exception.

    Code:
    mongoTemplate.save(show);
    or

    Code:
    mongoTemplate.insert(show);
    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:

    Code:
    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.

    Thanks

    -Matt
Working...
X