Announcement Announcement Module
No announcement yet.
mongodb: hard-coded mapping from 'id' to '_id' not appropriate in my case Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • mongodb: hard-coded mapping from 'id' to '_id' not appropriate in my case

    Spring data mongodb appears to force a property called 'id' to map to mongodb '_id'. Unfortunately, this is not the behaviour I need. I cannot rename my property. I want to keep my existing property 'id' as a field and introduce a new property 'docId' to map to mongodb '_id'. This is necessary to maintain compatibility with an existing hyperjaxb3-generated domain model that is also persisted to a relational database via hibernate.

    The relevant lines of code from 1.1.0.RELEASE that affect me are...
    private static final List<String> DEFAULT_ID_NAMES = Arrays.asList("id", "_id");
    There does not seem to be a way for me to injected or customise the behaviour I need. However, if I override the classes in my project and edit the above lines to exclude 'id', then everything works as I need it to. I then extend AbstractMongoEventListener onAfterConvert/onBeforeSave to map as I need. Using the listener allows me to keep using the existing deep mapping in MappingMongoConverter.

    Is there a more elegant solution?

    Shall I raise an improvement JIRA or does one already exist (a quick search found nothing)?

  • #2
    Hello again... any opinion from anyone who own the source code I quote above..?


    • #3
      According to the reference docs you can use the @Id annotation on any property to let that one become the id property.


      • #4 Yes, I tried this. It results in the error below...

        Caused by: ption: Attempt to add id property protected java.lang.Long but already have property protected java.lang.Long com.domain.object.docId registered as id. Check your mapping configuration!

        Note that I must maintain compatibility with the existing JPA annotations, which have a javax.persistence.Id annotation.


        • #5
          Any more advice..?

          I would rather not override your classes, as I believe I have to do now.