简体   繁体   English

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

[英]How to make a list (containing of objects) persistent

First of all, as you can see, I work in Java and specifically in NetBeans IDE. 首先,如您所见,我使用Java,特别是NetBeans IDE。 So, I have a class Person that extends two classes : Trainer, and Athlete. 因此,我有一个Person类,它扩展了两个类:Trainer和Athlete。

In the main, I create a new ArrayList list = new ArrayList(); 首先,我创建一个新的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. 在这里,这些列表正在运行并经过测试,可以,但是可以在Java级别而不是ObjectDB级别使用。

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 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();

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). 确保commit事务(尽管您提到实体已保留)。

  • 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: 如果您在TrainerAthlete之间的一对多关联是双向的(也许显示您的实体),请确保正确设置链接的“另一”侧,如下所示:

     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): 或在您的实体中添加方法(在此处的Trainer ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM