Uploaded image for project: 'Spring Data MongoDB'
  1. Spring Data MongoDB
  2. DATAMONGO-2339

Id property containing underscore not mapped correctly

    XMLWordPrintable

    Details

      Description

      If the name of the Java class property has '_' and this property is specified as an @Id (mongo Id for collection), then MongoRepository does not find the object by findById method.

      import lombok.AllArgsConstructor;
      import lombok.ToString;
      import org.springframework.boot.Banner;
      import org.springframework.boot.CommandLineRunner;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.annotation.Bean;
      import org.springframework.data.annotation.Id;
      import org.springframework.data.mongodb.core.mapping.Document;
      import org.springframework.data.mongodb.repository.MongoRepository;
      
      interface BadRepository extends MongoRepository<BadIdObj, String> {}
      interface GoodRepository extends MongoRepository<GoodIdObj, String> {}
      
      @Document("bad_id_objects")
      @ToString
      @AllArgsConstructor
      class BadIdObj {
          @Id
          private String bad_key;
      }
      
      @Document("good_id_objects")
      @AllArgsConstructor
      @ToString
      class GoodIdObj {
          @Id
          private String goodKey;
      }
      
      @SpringBootApplication
      public class Application {
          private static final String ID = "1";    
          public static void main(String[] args) {
              SpringApplication springApplication = new SpringApplication(Application.class);
              springApplication.run(args);
          }    
      
          @Bean
          public CommandLineRunner commandLineRunner(ApplicationContext context) {
              return args -> {
                  GoodRepository goodRepository = context.getBean(GoodRepository.class);
                  BadRepository badRepository = context.getBean(BadRepository.class);            
      
                  goodRepository.deleteAll();
                  badRepository.deleteAll();            
      
                  goodRepository.save(new GoodIdObj(ID));
                  badRepository.save(new BadIdObj(ID));            
      
                  System.out.println("Good=" + goodRepository.findById(ID).orElse(null));
                  System.out.println("Bad=" + badRepository.findById(ID).orElse(null));
              };
          }
      }
      

      Console out:

      Good=GoodIdObj{goodKey=1}
      Bad=null
      

        Attachments

          Activity

            People

            Assignee:
            cstrobl Christoph Strobl
            Reporter:
            lkv512 Kirill Leonov
            Last updater:
            Mark Paluch
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: