2010-11-09 56 views
0

首先,您可以看到,我使用的是Java,特别是NetBeans IDE。 所以,我有一个班级人员,扩展两个类:培训师和运动员。如何创建一个列表(包含对象)persistent

在主,我创建了一个新的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(); 

回答

0

但最终,尽管运动员和培训人员坚持不懈的进口,我有名单都没有。

一些想法:

  • 确保您commit交易(虽然你提到实体持续)。

  • 如果您一对多协会和Athlete之间Trainer是双向的(也许显示您的实体),请务必正确设置链接的“其他”的一面,就像这样:

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

    或在你的实体添加一个方法(在这里Trainer):

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

    ,更换上面的纹路搭配:

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

谢谢,看起来不错,而且很少工作,我必须要完美。非常感谢Pascal Thivent!顺便说一句,跆拳道/分钟度量好笑话。 :)))) – user501625 2010-11-09 10:20:26

相关问题