DB4O在将对象添加到作为对象列表的参数之一时不会更新Object

时间:2012-02-14 10:58:01

标签: db4o

我有一个名为serie的对象,它有3个args

private String name;
    private String picfile;
private Vector<Episode> episodes = new Vector<Episode>();

我找回了我正在寻找的系列,并为其添加了一集

然后将这一集添加到系列中 但在关闭ObjectContainer并重新启动程序后,系列会松开这一集。

希望有人可以提供帮助。

这是整个代码

public class Db4o {

/**
 * @param args the command line arguments
 */
static ObjectContainer db;
static final String path="C:\\Users\\naki\\Documents\\wordmanagement";

public static void afficher(ObjectContainer db)
{
    ObjectSet<Serie> result=db.queryByExample(Serie.class);
    while(result.hasNext())
    {
        System.out.println(result.next());
    }
}
public static void main(String[] args) throws MalformedURLException {
    // TODO code application logic here

        try{
       // new File(path).delete();
        db=Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), path);
        System.err.println("it's connectetd");
        Serie s1=new Serie();
        s1.setName("Kungfu Panda");

        File file=new File("E:\\Series\\Word pic\\250px-Kung_Fu_Panda_-_Legends_of_Awesomeness_logo.jpg");

        s1.setPicfile(file.toString());
        Episode episode=new Episode();
        episode.setNumbre(4);
        episode.setTitle("The magic Po");
        episode.setSerieName("Kungfu Panda");
        db.store(episode);
        s1.setEpisode(episode);
        db.store(s1);
        ObjectSet<Serie> result=db.queryByExample(new Serie("Kungfu Panda"));
        Serie s=result.next();
        s.setEpisode(episode);

        afficher(db);

        }
        finally
        {
            db.close();
        }

}


public class Serie {
private String name;
    private String picfile;
private Vector<Episode> episodes = new Vector<Episode>();

public String getPicfile() {
    return picfile;
}

public void setPicfile(String picfile) {
    this.picfile = picfile;
}

public Serie(String name) {
   this.name=name; 
}

public Vector<Episode> getEpisodes() {
    return episodes;
}

public void setEpisode(Episode e) {
    this.episodes.add(e);
}



public String getName() {
    return name;
}

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

@Override
public String toString() {
    return "Serie{" + "name=" + name + ", picfile=" + picfile + ", episodes=" + episodes + '}';
}


public class Episode {
private int numbre;
private String title;
    private String  serieName;
private ArrayList<Word> words = new ArrayList<Word>();

public Episode(int numbre, String titl, String  serieName,ArrayList<Word> words) {

    this.numbre = numbre;
    this.title = title;
    this.serieName=serieName;
     this.words = words;
}

public Episode() {

}

public String getSerieName() {
    return serieName;
}

public void setSerieName(String serieName) {
    this.serieName = serieName;
}


public int getNumbre() {
    return numbre;
}

public void setNumbre(int numbre) {
    this.numbre = numbre;
}


public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public ArrayList<Word> getWords() {
    return words;
}

public void setWords(ArrayList<Word> words) {
    this.words = words;
}

@Override
public String toString() {
    return "{" + "Episode=" + numbre + " , title=" + title + '}';
}

2 个答案:

答案 0 :(得分:0)

请检查您是否可能遇到更新深度问题(请查看此博文的最后一部分):

http://www.gamlor.info/wordpress/2009/09/db4o-activation-update-depth/

最佳。

答案 1 :(得分:0)

我看不到你在哪里关闭并重新打开数据库。

无论如何,我只是运行你的代码(例如重新打开数据库进行了一些更改),它确实正确地检索了Serie,意思是,它的剧集。

见下面的代码:

import java.io.*;
import java.net.*;
import java.util.*;

import com.db4o.*;

public class Db4o {

    static final String path = System.getenv("temp") + "\\test.odb";

    public static void afficher(ObjectContainer db) {
        ObjectSet<Serie> result = db.queryByExample(Serie.class);
        while (result.hasNext()) {
            System.out.println(result.next());
        }
    }

    public static void main(String[] args) throws MalformedURLException {
        ObjectContainer db = null;
        try {

            new File(path).delete();
            db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), path);
            System.err.println("it's connectetd");

            Serie s1 = new Serie("Kungfu Panda");
            File file = new File(
                    "E:\\Series\\Word pic\\250px-Kung_Fu_Panda_-_Legends_of_Awesomeness_logo.jpg");
            s1.setPicfile(file.toString());

            Episode episode = new Episode(4, "The magic Po", "Kungfu Panda");
            s1.setEpisode(episode);
            db.store(s1);    

            // Reopen the database
            db.close();    
            db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), path);

            afficher(db);

        } finally {
            db.close();
        }

    }
}

class Serie {
    private String name;
    private String picfile;
    private Vector<Episode> episodes = new Vector<Episode>();

    public String getPicfile() {
        return picfile;
    }

    public void setPicfile(String picfile) {
        this.picfile = picfile;
    }

    public Serie(String name) {
        this.name = name;
    }

    public Vector<Episode> getEpisodes() {
        return episodes;
    }

    public void setEpisode(Episode e) {
        this.episodes.add(e);
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Serie{" + "name=" + name + ", picfile=" + picfile
                + ", episodes=" + episodes + '}';
    }
}

class Episode {
    private int numbre;
    private String title;
    private String serieName;

    public Episode(int numbre, String titl, String serieName) {

        this.numbre = numbre;
        this.title = titl;
        this.serieName = serieName;
    }

    public Episode() {

    }

    public String getSerieName() {
        return serieName;
    }

    public void setSerieName(String serieName) {
        this.serieName = serieName;
    }

    public int getNumbre() {
        return numbre;
    }

    public void setNumbre(int numbre) {
        this.numbre = numbre;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String toString() {
        return "{" + "Episode=" + numbre + " , title=" + title + '}';
    }
}

请注意,在Episode构造函数中出现错误:

public Episode(int numbre, String titl, String serieName) {
    this.numbre = numbre;
    this.title = title; // WRONG. Need to be: this.title = titl;
    this.serieName = serieName;
}