dozd / mongo-mapper

Easy POJO codec for MongoDB in Java
Apache License 2.0
27 stars 9 forks source link

Error retrieving data with a POJO #10

Open yon-cuadrado opened 7 years ago

yon-cuadrado commented 7 years ago

Hello

I am using the following POJO to insert and retrieve data. The insert goes ok but the retrieval gives the following error:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.util.ArrayList.
    at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
    at com.mongodb.client.model.BuildersHelper.encodeValue(BuildersHelper.java:35)
    at com.mongodb.client.model.Filters$SimpleEncodingFilter.toBsonDocument(Filters.java:1078)
    at com.mongodb.client.model.Filters$AndFilter.toBsonDocument(Filters.java:915)
    at com.mongodb.FindIterableImpl.createQueryOperation(FindIterableImpl.java:229)
    at com.mongodb.FindIterableImpl.execute(FindIterableImpl.java:224)
    at com.mongodb.FindIterableImpl.first(FindIterableImpl.java:205)
    at com.serenity.common.helpers.MongoDbClientsCollectionMongoMapperHelper.getClient(MongoDbClientsCollectionMongoMapperHelper.java:74)
    at com.serenity.db.StepsDef.dbStepsDef.thisClientShouldBeFoundInTheDatabase(dbStepsDef.java:100)
    at ✽.Then this client should be found in the database(MongoDb.feature:11)

Main class of the POJO

package com.serenity.common.dtos.mongodb;

import eu.dozd.mongo.annotation.Entity;
import eu.dozd.mongo.annotation.Id;

import java.util.List;
import java.util.UUID;

/**
 * Created by demian on 6/11/17.
 */
@Entity
public class ClientsDtoMongoMapper
{
    // region getters and setters
    public String get_id()
    {
        return _id;
    }

    public void set_id(String _id)
    {
        this._id = _id;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public List<ClientAddressDtoMongoMapper> getAddresses()
    {
        return addresses;
    }

    public void setAddresses(List<ClientAddressDtoMongoMapper> addresses)
    {
        this.addresses = addresses;
    }
    // endregion

    @Id
    private String _id;

    private String firstName;
    private String lastName;

    public List<ClientAddressDtoMongoMapper> addresses;

    public ClientsDtoMongoMapper()
    {

    }

    public ClientsDtoMongoMapper(String firstName, String lastName, List<ClientAddressDtoMongoMapper> addresses)
    {
        this.set_id(UUID.randomUUID().toString());
        this.setFirstName(firstName);
        this.setLastName(lastName);
        this.addresses = addresses;
    }

    @Override
    public boolean equals(Object client)
    {

        if (this.get_id().equals(((ClientsDtoMongoMapper) client).get_id()) &&
                this.getFirstName().equals(((ClientsDtoMongoMapper) client).getFirstName()) &&
                this.getLastName().equals(((ClientsDtoMongoMapper) client).getLastName()) &&
                this.addresses.equals(((ClientsDtoMongoMapper) client).getAddresses()))
        {
            return true;
        }
        return false;
    }
}

Embedded addresses class

package com.serenity.common.dtos.mongodb;

import eu.dozd.mongo.annotation.Embedded;

/**
 * Created by demian on 6/11/17.
 */

@Embedded
public class ClientAddressDtoMongoMapper
{
    // region getters and setters
    public String getStreet()
    {
        return street;
    }

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

    public String getNumber()
    {
        return number;
    }

    public void setNumber(String number)
    {
        this.number = number;
    }

    public String getPostalCode()
    {
        return postalCode;
    }

    public void setPostalCode(String postalCode)
    {
        this.postalCode = postalCode;
    }
    // endregion

    private String street;
    private String number;
    private String postalCode;

    public ClientAddressDtoMongoMapper()
    {

    }

    public ClientAddressDtoMongoMapper(String street, String number, String postalCode)
    {
        this.street = street;
        this.number = number;
        this.postalCode = postalCode;
    }

    @Override
    public boolean equals(Object client)
    {
        if (this.getNumber().equals(((ClientAddressDtoMongoMapper) client).getNumber()) &&
                this.getPostalCode().equals(((ClientAddressDtoMongoMapper) client).getPostalCode()) &&
                this.getStreet().equals(((ClientAddressDtoMongoMapper) client).getStreet()))
        {
            return true;
        }
        return false;
    }
}

Function used to initialize the mongocollection variable

final MongoCollection mongoCollection;

    public MongoDbClientsCollectionMongoMapperHelper(String mongoDbConnectionString, String databaseName, String collectionName)
    {
        CodecRegistry codecRegistry = CodecRegistries.fromProviders(MongoMapper.getProviders());
        MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
        optionsBuilder.codecRegistry(codecRegistry);
        MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoDbConnectionString, optionsBuilder));

        MongoDatabase mongoDatabase = mongoClient.getDatabase(databaseName);
        this.mongoCollection = mongoDatabase.getCollection(collectionName, ClientsDtoMongoMapper.class);
    }

Code used to get the list of objects of type Client of the database

    public List<ClientsDtoMongoMapper> getClient(ClientsDtoMongoMapper client)
    {
        Bson filter = Filters.and(eq("lastName", client.getLastName()),
                eq("firstName", client.getFirstName()),
                eq("addresses", client.addresses));

        List<ClientsDtoMongoMapper> clients = mongoCollection.find(filter).into(new ArrayList<>());

        return clients;
    }