duda con Db4o con java

Iniciado por edu@85, 3 Marzo 2009, 13:35 PM

0 Miembros y 1 Visitante están viendo este tema.

edu@85

Lo que quiero hacer es crear una base de datos cerrarla luego abrirla y hacer una consulta. Las demas clases (player,team..) contienen atributos y metodos get y set.


Este es el codigo en java:

Código (java) [Seleccionar]
import com.db4o.*;
import com.db4o.query.*;

import java.io.File;


import java.util.Iterator;

public class Db4oTest {
    private static String filename;
   
   
    public Db4oTest() {
        filename = "c:/teamsdemo.yap";
    }
   
    public static void main(String[] args){
        Db4oTest tester = new Db4oTest();
        tester.createTestData();
        tester.queryByExampleDemo();
      
    }
   
    /**
     * creates test objects and stores them in new database file
     */
  
private void createTestData(){
        System.out.println("Creamos base de datos");
       
        // Create Players
        Player p1 = new Player("Barry Bonds", 25, 0.362f);
        Player p2 = new Player("Marquis Grissom", 9, 0.279f);
        Player p3 = new Player("Ray Durham", 5, 0.282f);
        Player p4 = new Player("Adrian Beltre", 29, 0.334f);
        Player p5 = new Player("Cesar Izturis", 3, 0.288f);
        Player p6 = new Player("Shawn Green", 15, 0.266f);
       
        // Create Pitchers
        Player p7 = new Pitcher("Kirk Rueter",46, 0.131f, 9);
        Player p8 = new Pitcher("Kazuhisa Ishii",17, 0.127f, 13);
       
        // Create Teams
        Team t1 = new Team("Giants", "San Francisco", 91, 71);
        Team t2 = new Team("Dodgers", "Los Angeles", 93, 69);
       
        // Add Players to Teams
        t1.addPlayer(p1); p1.setTeam(t1);
        t1.addPlayer(p2); p2.setTeam(t1);
        t1.addPlayer(p3); p3.setTeam(t1);
        t2.addPlayer(p4); p4.setTeam(t2);
        t2.addPlayer(p5); p5.setTeam(t2);
        t2.addPlayer(p6); p6.setTeam(t2);
       
        // Add Pitchers to Teams
        t1.addPlayer(p7); p7.setTeam(t1);
        t2.addPlayer(p8); p8.setTeam(t2);
       
        // Open a new database
        new File(filename).delete();
        ObjectContainer db = Db4o.openFile(filename);
       
        // Store Teams - also stores associated Players
        //db.set(t1);
        db.store(t1);
        System.out.println("Stored " + t1);
        //db.set(t2);
        db.store(t2);
        System.out.println("Stored " + t2);
       
        // Close database
       db.close();
       
}
   

   
    /**
     * Demonstrates query-by-example
     */

private void queryByExampleDemo(){
       
       
        // Open database
        ObjectContainer db = Db4o.openFile(filename);
        // Retrieve a Player by creating an example Player object
        Player examplePlayer = new Player("Barry Bonds",0,0f);
        ObjectSet result=db.queryByExample(examplePlayer);
      
        System.out.println(result.size());
        while(result.hasNext()) {
            System.out.println(result.next());
        }
        result.reset();
       
        // Retrieve a Team and all Players
        Team exampleTeam = new Team("Dodgers",null, 0, 0);
        result=db.queryByExample(exampleTeam);
       
        // List results
        System.out.println(result.size());
        while (result.hasNext()){
            Team t = (Team) result.next();
            System.out.println(t);
            Iterator itr = t.getPlayers().iterator();
            while (itr.hasNext()) {
                Player p = (Player) itr.next();
                System.out.println(p);
            }
        }
       
        // Close database
        db.close();
    }

   
   Salta el siguiente error:

Exception in thread "main" java.lang.ClassCastException: Player cannot be cast to com.db4o.reflect.generic.GenericObject
   at com.db4o.reflect.generic.GenericField.get(GenericField.java:36)
   at com.db4o.internal.FieldMetadata.getOn(FieldMetadata.java:582)
   at com.db4o.internal.FieldMetadata.collectConstraints(FieldMetadata.java:338)
   at com.db4o.internal.ClassMetadata$3.apply(ClassMetadata.java:447)
   at com.db4o.internal.ClassMetadata.forEachDeclaredAspect(ClassMetadata.java:1968)
   at com.db4o.internal.ClassMetadata.forEachAspect(ClassMetadata.java:1949)
   at com.db4o.internal.ClassMetadata.forEachField(ClassMetadata.java:1939)
   at com.db4o.internal.ClassMetadata.collectConstraints(ClassMetadata.java:443)
   at com.db4o.internal.query.processor.QConObject.associateYapClass(QConObject.java:82)
   at com.db4o.internal.query.processor.QConObject.byExample(QConObject.java:421)
   at com.db4o.internal.PartialObjectContainer.queryByExampleInternal(PartialObjectContainer.java:757)
   at com.db4o.internal.PartialObjectContainer.queryByExample(PartialObjectContainer.java:741)
   at com.db4o.internal.ExternalObjectContainer.queryByExample(ExternalObjectContainer.java:75)
   at com.db4o.internal.ExternalObjectContainer.get(ExternalObjectContainer.java:71)
   at Db4oTest.queryByExampleDemo(Db4oTest.java:102)
   at Db4oTest.main(Db4oTest.java:25)

sirdarckcat

No dijiste que es Player ni que hay en la linea 102
porque del codigo que mandaste la linea 102 no hace una QueryByExample
Suponiendo que la 102 es ObjectSet result=db.queryByExample(examplePlayer); entonces el problema esta en que examplePlayer no es una instancia ni interface de "GenericObject". Si la clase es generica debes poner el tipo entre <> en el contructor.

Probablemente despues de 7 dias ya resolviste la duda, pero es raro ver mensajes sin responder.. asi que..

edu@85

#2
Si esa es la linea 102, la clase player es esta:

Código (java) [Seleccionar]
public class Player {
   
    protected String name;
    protected int squadNumber;
    protected float battingAverage;
    protected Team team;
   
    public Player (String name, int squadNumber, float battingAverage){
        this.name = name;
        this.squadNumber = squadNumber;
        this.battingAverage = battingAverage;
    }
   
    public Player() {

// TODO Auto-generated constructor stub
}

public void setName(String n){this.name = n;} 
    public String getName(){return this.name;}

    public void setSquadNumber(int s){this.squadNumber = s;} 
    public int getSquadNumber(){return this.squadNumber;}   
   
    public void setBattingAverage(final float b){this.battingAverage = b;}
    public float getBattingAverage(){return this.battingAverage;}

    public void setTeam(Team t) {this.team = t;}
    public Team getTeam() {return this.team;}
   
    public String toString() {
        return name + ":" + battingAverage;
    }
}

el problema creo que esta cuando creo la base y la cierro en el metodo createTestData() y despues al hacer la consulta la abro y salta el error.

sirdarckcat

pon tu codigo entre [code=java][/code]

Cita de: http://developer.db4o.com/forums/thread/45980.aspx#ctl00_ctl01_bcr_ctl00___PostRepeater_ctl03_PostAnchorHi !

Whenever you get an unexpected GenericObject returned,
this is an issue with db4o not being able to access
your application classes.

On Java this is always a CLASSPATH issue.

On .NET this is always an issue with not having access
to the original assembly, because it possibly was
renamed.

In both cases it may work fine to store and access
objects in the first session, but it won't work any
longer after you reopen the database.


We see the above frequently here, so there should
definitely be an entry in our Reference Documentation
under Usage Pitfalls

http://developer.db4o.com/Resources/view.aspx/Reference/Usage_Pitfalls


Yeah, here is something about where not to place the
db4o Jar:

http://developer.db4o.com/Resources/view.aspx/Reference/Usage_Pitfalls/Classloader_And_Generic_Classes