如何使列表(包含对象)持久化

发布于 2024-10-01 11:24:40 字数 1161 浏览 0 评论 0原文

首先,如您所见,我使用 Java,特别是 NetBeans IDE。 所以,我有一个 Person 类,它扩展了两个类:Trainer 和 Athlete。

主要是,我创建一个新的 ArrayList list = new ArrayList();

,然后用我创建并持久化的对象填充该列表。

Trainer tr1=        new Trainer("George","White","England",5665);
Athlete ath1=       new Athlete("Mairy","Willians","France",1,'f',"21/3/1988",68,172,"France");
list.add(ath1);
Athlete ath2=new Athlete("Iggy","Black","USA",2,'f',"10/4/1988",70,175,"U.S.A.");
list.add(ath2);
tr1.setAthletes(list);

(这些字段分别在训练师和运动员类别的构造函数中得到了很好的定义。

我也让它们持久化。

em2.persist(tr1);
em2.persist(ath1);
em2.persist(ath2);

但最终,尽管运动员和训练师是持久化的,但我的列表却没有。

这就是我的问题所在开始,我希望这些列表是持久的。

但是它们很适合在 Java 级别,而不是在 ObjectDB 级别,

我应该重新开始吗?

这里,这些列表正在工作并经过测试,并且没有问题, 和这些人一起吗?我真的需要帮助,这很严重

PS:当然,我已经制作了所需的进口产品,例如。

import javax.persistence.*;
import java.util.*;

EntityManagerFactory emf2 = Persistence.createEntityManagerFactory("$objectdb/db/personas2.odb");
EntityManager em2 = emf2.createEntityManager();
em2.getTransaction().begin();
em2.close();
emf2.close();

First of all, as you can see, I work in Java and specifically in NetBeans IDE.
So, I have a class Person that extends two classes : Trainer, and Athlete.

In the main, I create a new ArrayList list = new ArrayList();

and then, i fill the list with objects that I've created and made persistent.

Trainer tr1=        new Trainer("George","White","England",5665);
Athlete ath1=       new Athlete("Mairy","Willians","France",1,'f',"21/3/1988",68,172,"France");
list.add(ath1);
Athlete ath2=new Athlete("Iggy","Black","USA",2,'f',"10/4/1988",70,175,"U.S.A.");
list.add(ath2);
tr1.setAthletes(list);

(Those fields, are well defined in the constructor of the classes trainer, and athlete respectively.

I also make them persistent.

em2.persist(tr1);
em2.persist(ath1);
em2.persist(ath2);

But in the end, despite of Athletes and Trainers being persistent, the lists that I have are not.

Thats where my problem starts, I want those lists to be persistent.

Here, those lists are working and tested and there are ok, but they are good to go in Java level, and not in ObjectDB level.

Should i start over?

Anyone that can help me out with this guys? I really need help, that's serious.

PS: Of course, I have made the imports that are needed such as

import javax.persistence.*;
import java.util.*;

EntityManagerFactory emf2 = Persistence.createEntityManagerFactory("$objectdb/db/personas2.odb");
EntityManager em2 = emf2.createEntityManager();
em2.getTransaction().begin();
em2.close();
emf2.close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒解孤独 2024-10-08 11:24:40

但最终,尽管运动员和教练员坚持不懈,但我的清单却并非如此。

一些想法:

  • 确保您提交事务(尽管您提到实体是持久化的)。

  • 如果 TrainerAthlete 之间的一对多关联是双向的(可能会显示您的实体),请确保设置正确链接的“另一”侧,如下所示:

    Trainer tr1 = new Trainer("乔治","怀特","英格兰",5665);
    运动员 ath1 = new Athlete("Mairy","Willians","法国",1,'f',"21/3/1988",68,172,"法国");
    ath1.setTrainer(tr1); // 设置链接的另一边
    列表.add(ath1);
    ...
    tr1.setAthletes(列表);
    

    或在您的实体中添加一个方法(在此处的 Trainer 中):

    public void addToAthletes(运动员) {
        运动员.setTrainer(this);
        this.athletes.add(运动员);
    }
    

    并将上面的行替换为:

    Trainer tr1 = new Trainer("乔治","怀特","英格兰",5665);
    运动员 ath1 = new Athlete("Mairy","Willians","法国",1,'f',"21/3/1988",68,172,"法国");
    tr1.addToAthletes(ath1);
    

But in the end, despite of Athletes and Trainers being persistent, the lists that I have are not.

Some ideas:

  • Make sure that you commit the transaction (although you mentioned that entities are persisted).

  • If your one to many association between Trainer and Athlete is bi-directional (maybe show your entities), make sure to set the "other" side of the link correctly, like this:

    Trainer tr1 = new Trainer("George","White","England",5665);
    Athlete ath1 = new Athlete("Mairy","Willians","France",1,'f',"21/3/1988",68,172,"France");
    ath1.setTrainer(tr1); // set the other side of the link
    list.add(ath1);
    ...
    tr1.setAthletes(list);
    

    or add a method in your entity (in Trainer here):

    public void addToAthletes(Athlete athlete) {
        athlete.setTrainer(this);
        this.athletes.add(athlete);
    }
    

    and replace the above lines with:

    Trainer tr1 = new Trainer("George","White","England",5665);
    Athlete ath1 = new Athlete("Mairy","Willians","France",1,'f',"21/3/1988",68,172,"France");
    tr1.addToAthletes(ath1);
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文