Fabio B.
Fabio B.

Reputation: 9410

Morphia storing empty values

I am using Morphia+MongoDB as a backend for a simple CRUD input mask.

I prepare the framework the simplest possible way, creating a new MongoClient and initializing Morphia like that:

Morphia morphia = new Morphia();
morphia.mapPackage("it.trew.omg.model");

Both instances are injected into a DAO:

public class ClientiDao extends BasicDAO<Cliente, String>

My entity is still pretty simple:

@Entity("clienti")
public class Cliente {

    @Id ObjectId id;

    String name;
    String address;
    String city;
    String state;
    String email;

    public Cliente() {

    }

// getters+setters

}

Let's say I create a Cliente by just filling the name inside my form.

When a controller calls the save method, the operation is successful:

getClientiDao().save(cliente);

But when I query the 'clienti' collection from the mongo console I get this:

{ "_id" : ObjectId("547edf630364677dd2f911b8"), "className" : "it.trew.omg.model.Cliente", "name" : "Fabio Bozzo", "indirizzo" : "", "citta" : "", "cap" : "", "provincia" : "", "stato" : "", "email" : "", "telefono" : "", "fax" : "", "note" : "" }

I thought that empty fields would not have been inserted. Is there something wrong?

Versions are:

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

        <dependency>
            <groupId>org.mongodb.morphia</groupId>
            <artifactId>morphia</artifactId>
            <version>0.108</version>
        </dependency>

Upvotes: 1

Views: 761

Answers (1)

evanchooly
evanchooly

Reputation: 6243

Empty fields mean null. Those fields have "" values and so are persisted. If you don't want them persisted, they'd need to be null.

Upvotes: 4

Related Questions