如何使列表(包含对象)持久化
首先,如您所见,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些想法:
确保您
提交
事务(尽管您提到实体是持久化的)。如果
Trainer
和Athlete
之间的一对多关联是双向的(可能会显示您的实体),请确保设置正确链接的“另一”侧,如下所示:或在您的实体中添加一个方法(在此处的
Trainer
中):并将上面的行替换为:
Some ideas:
Make sure that you
commit
the transaction (although you mentioned that entities are persisted).If your one to many association between
Trainer
andAthlete
is bi-directional (maybe show your entities), make sure to set the "other" side of the link correctly, like this:or add a method in your entity (in
Trainer
here):and replace the above lines with: