Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2720

Support Enums in query helpers

    • Type: Icon: New Feature New Feature
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.6.0
    • Component/s: Codecs, POJO
    • Labels:
      None

      Hi,

      Let's assume we have the following POJO:

      public final class Person {
      
          private final String  name;
          private final Country country;
      
          @BsonCreator
          public Person(
                  @BsonProperty("name") String name,
                  @BsonProperty("country") Country country) {
      
              this.name = name;
              this.country = country;
          }
      
          @BsonId
          public String getName() {
              return name;
          }
      
          public Country getCountry() {
              return country;
          }
      
          public enum Country {
              IRELAND,
              POLAND
          }
      }
      

      Now we create a related collection:

      MongoCollection<Person> collection = new MongoClient()
              .getDatabase("database")
              .getCollection("people", Person.class)
              .withCodecRegistry(fromRegistries(
                      getDefaultCodecRegistry(),
                      fromProviders(
                              PojoCodecProvider
                                      .builder()
                                      .automatic(true) // note the automatic POJO support
                                      .build()
                      )
              ));
      

      Everything is fine when you do simple CRUD operations. But try to do the following:

      Person person = collection
              .find(eq("country", POLAND))
              .first();
      

      You will get org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.gat.lib.mongo.Person$Country

      In such cases you need to register an enum codec manually. I think it's confusing, especially as the documentation says that "enums are fully supported" while they are limited to simple CRUD.

            Assignee:
            Unassigned Unassigned
            Reporter:
            cbartosiak Cezary Bartosiak
            Votes:
            4 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: