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:
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)
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)