1

I'm doing a test with DB4O. I am trying to insert data. The problem I have is:

When I insert the name of winery and D.O. I have to insert only if not exist in the database.

How could i do it?

This is my insert code:

Scanner teclado=new Scanner(System.in);

        try{

        ObjectContainer db= Db4oEmbedded.openFile("DB4O.yap");
        System.out.print("Name of Wine: ");
        String nomVino = teclado.next();
        System.out.print("Name of Cellar: ");
        String nomBodega = teclado.next();
        System.out.print("D.O: ");
        String DO = teclado.next();

        System.out.print("Type of Wine: ");
        String tipoVino = teclado.next();
        System.out.print("Grad: ");
        float gradosAlch = teclado.nextFloat();
        System.out.print("Year of wine: ");
        int fecha = teclado.nextInt();

        teclado.close();

            Vinos v = new Vinos(nomVino,new Bodega(nomBodega,DO),tipoVino,gradosAlch,fecha);
            db.store(v); //guardar objetos
            db.commit(); // valida los datos
            //odb.rollback(); // deshace los datos
            System.out.print("Vino Introducido\n");
            db.close(); //cerrar la bd  
        }

     catch(Exception e){System.out.println (e);}

    }

Thank you in advance!

Funereal
  • 653
  • 2
  • 11
  • 32

1 Answers1

1

What is exactly your problem (your question is not clear) ?

Do you want to avoid duplicating Bodega() objects, i.e, when you store a new Vinos object you do not want to store another Bodega() object if one exists with bodegaName änd DO ?

If this is what you are trying to accomplish, you need to search the Bodega object in the database before using it with a Vinos object; something like:

Scanner teclado=new Scanner(System.in);

try{
    ObjectContainer db= Db4oEmbedded.openFile("DB4O.yap");
    System.out.print("Name of Wine: ");
    String nomVino = teclado.next();
    System.out.print("Name of Cellar: ");
    String nomBodega = teclado.next();
    System.out.print("D.O: ");
    String DO = teclado.next();

    System.out.print("Type of Wine: ");
    String tipoVino = teclado.next();
    System.out.print("Grad: ");
    float gradosAlch = teclado.nextFloat();
    System.out.print("Year of wine: ");
    int fecha = teclado.nextInt();

    teclado.close();

    ObjectSet<Bodega> foundBodegas = db.query(new Predicate<Bodega>() {
                                        @Override public  boolean match(Bodega candidate) {
                                             return candidate.getName().equals(nomeBodega);
                                        }});

    Bodega bodega = null;

    if (foundBodegas.size() == 1) {
        bodega = foundBodegas.next();
    }
    else {
        bodega = new Bodega(nomBodega, DO);
    }

    Vinos v = new Vinos(nomVino,  bodega  ,tipoVino,gradosAlch,fecha);

    db.store(v); //guardar objetos
    db.commit(); // valida los datos
    //odb.rollback(); // deshace los datos
    System.out.print("Vino Introducido\n");
    db.close(); //cerrar la bd  
}
catch(Exception e)
{
    System.out.println (e);
}

You can find more information here and here

Vagaus
  • 4,174
  • 20
  • 31