0

I am using Spring MVC 5.2.6 to create a simple reservation system. I noticed that in my case (I do not need much scalability) a usage of @DBRef with mongodb is a good solution. However I am facing a very weird issue. org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.DBRef Domain model file User.java:

@Document(collection = "users")
public class User {
   @Id
   private String ID;
   @Field
   private String name;
   @DBRef
   private Address address;
   @Field
   private String phone;
   @Field
   private String email;

   public User() {}
   public User(final String name, final Address address, final String phone, final String email) {
      this.name = name;
      this.phone = phone;
      this.address= address;
      this.email = email;
   }

   //setters and getters
}

Address.java:

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

@Document(collection = "addresses")
public class Address {
    @Id
    private String id;
    @Field
    private String street;
    @Field
    private String zipCode;
    @Field
    private String building;
    @Field
    private String city;

    public Address() {
    }

    public Address(final String street, final String zipCode, final String building, final String city) {
        this.street = street;
        this.zipCode = zipCode;
        this.building = building;
        this.city = city;
    }

    public String getCity() {
        return city;
    }

    public void setCity(final String city) {
        this.city = city;
    }

    public String getId() {
        return id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(final String street) {
        this.street = street;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(final String zipCode) {
        this.zipCode = zipCode;
    }

    public String getBuilding() {
        return building;
    }

    public void setBuilding(final String building) {
        this.building = building;
    }
}

MongoConfiguration has these guys inside:

    @Bean
    public MappingMongoConverter mappingMongoConverter(final MongoDbFactory mongoDbFactory,
            final MongoMappingContext mongoMappingContext) {
        final DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
        final MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
        converter.setTypeMapper(new DefaultMongoTypeMapper(null));
        converter.setCodecRegistryProvider(mongoDbFactory);

        return converter;
    }

    @Bean
    public MongoTemplate mongoTemplate(final MongoDbFactory dbFactory,
            final MappingMongoConverter mappingMongoConverter) throws Exception {
        final MongoTemplate template = new MongoTemplate(dbFactory, mappingMongoConverter);
        return template;
    }

The problem occurs only, when email address is requested with findByEmail. When findByPhone is used the result will be return normally without an issue.

MongoDB query from console shows, that both phone number and email are after the address reference.

In my pom.xml I have this dependencies:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>2.2.8.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.11.2</version>
    </dependency>

Why does the exception happen, when search is used by only one specific field?

halfer
  • 19,824
  • 17
  • 99
  • 186
user1415536
  • 236
  • 1
  • 14
  • 26
  • I would put a bounty on this question, but this will leave me without any reputation points :) – user1415536 Jun 25 '20 at 12:17
  • You cannot put a bounty on the question for two days anyway. – halfer Jun 25 '20 at 16:47
  • And I am out of points anyway :) Let's just pretend that everything works fine :) To me it seems like a very weird bug. – user1415536 Jun 25 '20 at 16:56
  • As a work-around (it was mentioned a lot in posts about @DBRef), just use some kind of id inside the main object. It will add one more setter/getter to your class, but at least it will work. Also, it seems like an application is being more scalable in this way from the mongo point of view (you can keep collections in two different databases, which I do not want to do for my hobby project. Really I do not get any money from it. I am doing this for fun on my vacations). – user1415536 Jun 27 '20 at 08:55

0 Answers0