DB4O不';在将对象添加到其作为对象列表的参数时,不要更新对象
我有一个叫做serie的对象,它有3个参数DB4O不';在将对象添加到其作为对象列表的参数时,不要更新对象,db4o,Db4o,我有一个叫做serie的对象,它有3个参数 private String name; private String picfile; private Vector<Episode> episodes = new Vector<Episode>(); 私有字符串名称; 私有字符串文件; 私有向量集=新向量(); 我找回了我正在寻找的意甲联赛,并在其中添加了一集 然后,这一集被加入了意甲 但在关闭ObjectContainer并重新启动该程序后,该联盟失去了这一插
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 + '}';
}
公共类Db4o{
/**
*@param指定命令行参数
*/
静态对象容器数据库;
静态最终字符串路径=“C:\\Users\\naki\\Documents\\wordmanagement”;
公共静态无效附加器(ObjectContainer db)
{
ObjectSet result=db.queryByExample(Serie.class);
while(result.hasNext())
{
System.out.println(result.next());
}
}
publicstaticvoidmain(字符串[]args)引发畸形的DurLexException{
//此处的TODO代码应用程序逻辑
试一试{
//新建文件(路径).delete();
db=Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(),path);
System.err.println(“它是connectetd”);
系列s1=新系列();
s1.设定名称(“功夫熊猫”);
File File=新文件(“E:\\Series\\Word pic\\250px-Kung\u Fu\u Panda\u-\u Legends\u of\u awemomeness\u logo.jpg”);
s1.setPicfile(file.toString());
插曲=新插曲();
第四集:塞特纳姆布雷(4);
剧集集名称(“魔幻Po”);
插曲名称(“功夫熊猫”);
数据库存储(插曲);
s1.一集(一集);
数据库存储(s1);
ObjectSet result=db.queryByExample(新系列(“功夫熊猫”);
Serie s=result.next();
s、 一集(一集);
阿菲彻(db);
}
最后
{
db.close();
}
}
公务舱{
私有字符串名称;
私有字符串文件;
私有向量集=新向量();
公共字符串getPicfile(){
返回picfile;
}
公共void setPicfile(字符串picfile){
this.picfile=picfile;
}
公共序列(字符串名称){
this.name=name;
}
公共向量机{
回归集;
}
第五集(第五集){
本条第1.1段添加(e);
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
@凌驾
公共字符串toString(){
返回“Serie{“+”name=“+name+”,picfile=“+picfile+”,scents=“+scents+”}”;
}
公共课插曲{
私人住宅;
私有字符串标题;
私有字符串序列号;
private ArrayList words=new ArrayList();
公共插曲(int numbre、String titl、String serieName、ArrayList words){
this.numbre=numbre;
this.title=标题;
这个。丝氨酸胺=丝氨酸胺;
这个单词=单词;
}
公共插曲(){
}
公共字符串getSerieName(){
返回丝氨酸胺;
}
public void setSerieName(字符串serieName){
this.serieName=serieName;
}
public int getNumbre(){
返回numbre;
}
公共无效设置编号(int numbre){
this.numbre=numbre;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共数组列表getWords(){
返回单词;
}
公共无效设置字(ArrayList字){
这个单词=单词;
}
@凌驾
公共字符串toString(){
返回“{”+”插曲=“+numbre+”,title=“+title+'}”;
}
请检查更新深度是否有问题(查看本文最后一部分):
最好。我看不出你在哪里关闭和重新打开数据库 无论如何,我只是运行了您的代码(例如,对重新打开数据库进行了一些更改),它确实正确地检索了序列,也就是说,包含了它的片段 请参见下面的代码:
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 + '}';
}
}
我关闭和重新打开的方式是停止并重新启动程序。即使我修复了插曲构造函数,我仍然面临同样的问题。如果我更改序列名称,它确实会持续,但如果我将一个插曲添加到该序列的插曲列表中,它不会持续该插曲。
public Episode(int numbre, String titl, String serieName) {
this.numbre = numbre;
this.title = title; // WRONG. Need to be: this.title = titl;
this.serieName = serieName;
}