title: How can solve the UniqueFieldConstraintViolationException db4o
body: <p>The code run ok, but give me a Exception that I can't controlle, This is the console message:</p>
  
  <pre><code>The person with this DNI es already exists
  Exception in thread "main" com.db4o.constraints.UniqueFieldValueConstraintViolationException: class: Vehiculo field: matricula
      at com.db4o.constraints.UniqueFieldValueConstraint$1.ensureSingleOccurence(UniqueFieldValueConstraint.java:66)
      at com.db4o.constraints.UniqueFieldValueConstraint$1.onEvent(UniqueFieldValueConstraint.java:97)
      at com.db4o.internal.events.Event4Impl.trigger(Event4Impl.java:78)
      at com.db4o.internal.events.EventRegistryImpl$4.run(EventRegistryImpl.java:123)
      at com.db4o.foundation.DynamicVariable.with(DynamicVariable.java:54)
      at com.db4o.internal.InCallback.run(InCallback.java:24)
      at com.db4o.internal.events.EventRegistryImpl.withExceptionHandlingInCallback(EventRegistryImpl.java:279)
      at com.db4o.internal.events.EventRegistryImpl.commitOnStarted(EventRegistryImpl.java:121)
      at com.db4o.internal.LocalTransaction.dispatchCommittingCallback(LocalTransaction.java:89)
      at com.db4o.internal.LocalTransaction.commit(LocalTransaction.java:66)
      at com.db4o.internal.LocalTransaction.commit(LocalTransaction.java:59)
      at com.db4o.internal.LocalObjectContainer.commitTransaction(LocalObjectContainer.java:689)
      at com.db4o.internal.LocalObjectContainer.close2(LocalObjectContainer.java:94)
      at com.db4o.internal.ObjectContainerBase.close1(ObjectContainerBase.java:361)
      at com.db4o.internal.ObjectContainerBase.close(ObjectContainerBase.java:344)
      at Principal.main(Principal.java:38)
  
  
  import com.db4o.Db4oEmbedded;
  import com.db4o.ObjectContainer;
  import com.db4o.ObjectSet;
  import com.db4o.config.EmbeddedConfiguration;
  import com.db4o.constraints.UniqueFieldValueConstraint;
  import com.db4o.constraints.UniqueFieldValueConstraintViolationException;
  import com.db4o.query.Query;
  
  
  public class Principal {
  
      public static void main(String[] args) {
  
          EmbeddedConfiguration config = Db4oEmbedded.newConfiguration();
          config.common().objectClass(Persona.class).objectField("dni").indexed(true);
          config.common().add(new UniqueFieldValueConstraint(Persona.class, "dni"));
  
  
          ObjectContainer bbdd = Db4oEmbedded.openFile(config, "databaserrr.db4o");
  
          Persona p = new Persona("Pepe2", 25, "50667847L");
  
          try{
          bbdd.store(p);
          bbdd.commit();
          System.out.println("Se ha guardado correctamente");
          Query q = bbdd.query();
          q.constrain(Persona.class);
          ObjectSet&lt;Persona&gt; res = q.execute();
          }catch (UniqueFieldValueConstraintViolationException e) {
          System.out.println("The person with this DNI es already exists");
          }finally{
              bbdd.close();
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>Persona Class
  
  
  public class Persona {
      //Cremos atributos
      private String dni;
      private String nombre = null;
      private int edad;
  
  
      public Persona() {
      }
      //Constructor
      public Persona(String n, int e, String d) {
          nombre = n;
          edad = e;
          dni = d;
  
      }
  
      //Getter y setters
      public String getDni() {
          return dni;
      }
  
      public String getNombre() {
          return nombre;
      }
  
      public int getEdad() {
          return edad;
      }
  
      public void setDni(String d) {
          dni = d;
      }
  
      public void setNombre(String n) {
          nombre = n;
      }
  
      public void setEdad(int e) {
          edad = e;
      }
  }
  </code></pre>