Thank you for your help,
but the Table a_b in the database is empty.
 

Werner Guttmann wrote:

 Ghislain,

iN class B, you will have to initialize the List like you have done for class A. Just change

public class B
{ private String id_b;
private ArrayList listA;

to

public class B
{ private String id_b;
private ArrayList listA = new ArrayList();

and everything should work fine.

Regards
Werner

Tue, 01 Jul 2003 19:06:23 +0200, Ghislain ESCORNE wrote:

>Please help me.
>My problem :
>
>I have two class : A.java et B.java with a relation n-m.
>
>package bd;
>import java.util.*;
>import java.io.*;
>
>public class A
>{ private String id_a;
> private ArrayList listB = new ArrayList();
>
> public A(){}
>
>public void setId_a (String id_a){
> this.id_a = id_a;
>}
>
>public String getId_a(){
> return id_a;
>}
>
> public void addListB(B b){
> listB.add(b);
> }
>
> public ArrayList getListB (){
> return listB;
> }
>
>public void setListB(ArrayList listB){
> this.listB = listB;
>}
>
>}
>
>
>
>package bd;
>import java.util.*;
>import java.io.*;
>
>public class B
>{ private String id_b;
> private ArrayList listA;
>
>public B(){}
>
>
>public void setId_b (String id_b){
> this.id_b = id_b;
>}
>
>public String getId_b(){
> return id_b;
>}
>
>public void addListA(A a){
> listA.add(a);
>}
>
>
> public ArrayList getListA (){
> return listA;
> }
>
>public void setListA(ArrayList listA){
> this.listA = listA;
> }
>
>
>The mapping file :
><?xml version="1.0"?>
><mapping>
> <class name="bd.A" identity="Id_a" >
> <map-to table="a" />
> <field name="Id_a" type="string">
> <sql name="id_a" type="char" />
> </field>
>
>
> <field name="ListB" type="bd.B" collection="arraylist">
>
> <sql name="id_b" many-table="a_b"
>many-key="id_b" />
> </field>
>
> </class>
>
>
>
>
> <class name="bd.B" identity="Id_b">
> <map-to table="b" />
> <field name="Id_b" type="string">
> <sql name="id_b" type="char" />
> </field>
>
>
> <field name="ListA" type="bd.A" collection="arraylist" >
> <sql name="id_a" many-table="a_b"
>many-key="id_a" />
> </field>
>
> </class>
>
>
>
></mapping>
>
>And the sql file :
>
>DROP TABLE B CASCADE ;
>DROP TABLE A CASCADE;
>DROP TABLE A_B;
>
>CREATE TABLE A (
> id_a varchar(200)
>);
>
>CREATE TABLE B (
> id_b varchar(200)
>);
>
>CREATE TABLE A_B(
> id_a varchar(200) ,
> id_b varchar(200) ,
> PRIMARY KEY (id_a,id_b)
>);
>
>The java Test :
>package bd;
>import org.exolab.castor.jdo.*;
>import org.exolab.castor.mapping.*;
>import org.exolab.castor.jdo.Database;
>import javax.transaction.*;
>import org.exolab.castor.jdo.JDO;
>import java.lang.*;
>import java.util.*;
>
>public class TestAB{
> public TestAB (){
>
> try{
> JDO.loadConfiguration(
>"/mnt/data/vicmasa/bd/testab.xml" );
> }catch (MappingException m){
> System.out.println("Execption: " +m);
> }
>
> JDO jdo;
> Database db;
> jdo = new JDO ("test");
>
> System.out.println(jdo.getDatabaseName());
> try {
> db = jdo.getDatabase();
> db.begin();
> A a = new A();
> a.setId_a("a");
> db.create(a);
> db.commit();
>
> db.begin();
> B b = new B();
> b.setId_b("b");
> b.addListA(a);
> a.addListB(b);
> db.create(b);
> db.commit();
> db.close();
>
> }catch (PersistenceException e) {
> System.out.println("Execption: " +e);
> }
>
> }
>}
>
>
>and The output :
>
>test
>Exception in thread "main" java.lang.NullPointerException
> at bd.B.addListA(B.java:23)
> at bd.TestAB.<init>(TestAB.java:35)
> at RunAB.main(RunAB.java:9)
>
>and the database a_b is empty.
>
>-----------------------------------------------------------
>If you wish to unsubscribe from this mailing, send mail to
>[EMAIL PROTECTED] with a subject of:
> unsubscribe castor-dev
>
 
 
 
 

Reply via email to