Hello everyone,

I've been trying to use OJB lately but I'm a newbie and I'm stuck with handling simple non decomposed M:N relationships.

Here's my purpose :

A <- m:n -> B
A <- m:n -> C

*** Mapping is :

<class-descriptor class="my.test.business.A"
   table="TBLA">
   <field-descriptor name="id" column="ID" jdbc-type="BIGINT"
       nullable="false" indexed="true" primarykey="true"
       autoincrement="true">
   </field-descriptor>
<field-descriptor name="att1" column="ATT1" jdbc-type="BIGINT" nullable="false" indexed="true">
   </field-descriptor>
<field-descriptor name="att2" column="ATT2" jdbc-type="VARCHAR" nullable="true" indexed="false">
   </field-descriptor>
<collection-descriptor name="bs" collection-class="org.apache.ojb.broker.util.collections.ManageableArrayList"
       element-class="my.test.business.B"
       auto-retrieve="false" auto-update="link"
       indirection-table="TBLAB">
       <fk-pointing-to-this-class column="A_ID" />
       <fk-pointing-to-element-class column="B_ID" />
   </collection-descriptor>
<collection-descriptor name="cs" collection-class="org.apache.ojb.broker.util.collections.ManageableArrayList"
       element-class="my.test.business.C"
       auto-retrieve="false" auto-update="link"
       indirection-table="TBLAC">
       <fk-pointing-to-this-class column="A_ID" />
       <fk-pointing-to-element-class column="C_ID" />
   </collection-descriptor>
</class-descriptor>

<class-descriptor class="my.test.business.B"
   table="TBLB">
   <field-descriptor name="id" column="ID" jdbc-type="BIGINT"
       nullable="false" indexed="true" primarykey="true"
       autoincrement="true">
   </field-descriptor>
   <field-descriptor name="label" column="LABEL"
       jdbc-type="VARCHAR" nullable="false" indexed="false">
   </field-descriptor>
   <collection-descriptor name="as"
collection-class="org.apache.ojb.broker.util.collections.ManageableArrayList"
       element-class="my.test.business.A"
       auto-retrieve="false" auto-update="link"
       indirection-table="TBLAB">
       <fk-pointing-to-this-class column="B_ID" />
       <fk-pointing-to-element-class column="A_ID" />
   </collection-descriptor>
</class-descriptor>

<class-descriptor class="my.test.business.C"
   table="TBLC">
   <field-descriptor name="id" column="ID" jdbc-type="BIGINT"
       nullable="false" indexed="true" primarykey="true"
       autoincrement="true">
   </field-descriptor>
   <field-descriptor name="label" column="LABEL"
       jdbc-type="VARCHAR" nullable="false" indexed="false">
   </field-descriptor>
   <collection-descriptor name="as"
collection-class="org.apache.ojb.broker.util.collections.ManageableArrayList"
       element-class="my.test.business.A"
       auto-retrieve="false" auto-update="link"
       indirection-table="TBLAC">
       <fk-pointing-to-this-class column="C_ID" />
       <fk-pointing-to-element-class column="A_ID" />
   </collection-descriptor>
</class-descriptor>

*** DDL is :

CREATE TABLE SEQ (
 TABLENAME varchar(250) NOT NULL,
 MAX_KEY bigint(20) default NULL,
 GRAB_SIZE int(11) default NULL,
 VERSION int(11) default NULL,
 PRIMARY KEY  (TABLENAME)
)

CREATE TABLE TBLA (
 ID bigint(20) unsigned NOT NULL auto_increment,
 ATT1 bigint(20) unsigned NOT NULL default '0',
 ATT2 varchar(255) default NULL,
 PRIMARY KEY  (ID)
)

CREATE TABLE TBLB (
 ID bigint(20) unsigned NOT NULL auto_increment,
 LABEL varchar(255) default NULL,
 PRIMARY KEY  (ID)
)

CREATE TABLE TBLC (
 ID bigint(20) unsigned NOT NULL auto_increment,
 LABEL varchar(255) default NULL,
 PRIMARY KEY  (ID)
)

CREATE TABLE TBLAB (
 A_ID bigint(20) unsigned NOT NULL default '0',
 B_ID bigint(20) unsigned NOT NULL default '0',
 PRIMARY KEY (A_ID,B_ID),
 CONSTRAINT FKAB1 FOREIGN KEY (A_ID) REFERENCES A (ID),
 CONSTRAINT FKAB2 FOREIGN KEY (B_ID) REFERENCES B (ID)
)

CREATE TABLE TBLAC (
 A_ID bigint(20) unsigned NOT NULL default '0',
 C_ID bigint(20) unsigned NOT NULL default '0',
 PRIMARY KEY (A_ID,C_ID),
 CONSTRAINT FKAC1 FOREIGN KEY (A_ID) REFERENCES A (ID),
 CONSTRAINT FKAC2 FOREIGN KEY (C_ID) REFERENCES C (ID)
)

*** and code is :

public class A implements Serializable {
 private Long id;
 private Long att1;
 private String att2;
 private List bs = new ArrayList();
 private List cs = new ArrayList();
 /* and getters/setters */
}

public class B implements Serializable {
 private Long id;
 private String label;
 private List as = new ArrayList();
 /* and getters/setters */
}

public class C implements Serializable {
 private Long id;
 private String label;
 private List as = new ArrayList();
 /* and getters/setters */
}

***

You might have noticed the collections auto-retrieve setting is set to 'false'. If I set this setting to true in *A* class mapping, I get a NullPointerException when I try to read instances of A.

Furthermore, auto-retrieve set to false, I can read and add new objects but I also get NullPointerExceptions when trying to update an object already stored.

Is there anything I'm doing wrong ? Please, do not hesitate to ask me questions. Any help would be greatly appreciated.

Many thanks.
Ramzi Oueslati

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to