Results 1 to 5 of 5

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

Hybrid View

  1. #1
    Join Date
    Jul 2006
    Posts
    13

    Default 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...

    QueryMapper.java:47
    Code:
    private static final List<String> DEFAULT_ID_NAMES = Arrays.asList("id", "_id");
    BasicMongoPersistentProperty.java:53
    Code:
    SUPPORTED_ID_PROPERTY_NAMES.add("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. #2
    Join Date
    Jul 2006
    Posts
    13

    Default

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

  3. #3
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    493

    Default

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

  4. #4
    Join Date
    Jul 2006
    Posts
    13

    Default

    org.springframework.data.annotation.Id? Yes, I tried this. It results in the error below...

    Caused by: org.springframework.data.mapping.model.MappingExce ption: Attempt to add id property protected java.lang.Long com.domain.object.id 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. #5
    Join Date
    Jul 2006
    Posts
    13

    Default

    Any more advice..?

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •