Two ways to correct:
1) Define b as "dependent" on a; this is done in the mapping file and is
pretty well documeneted (see FAQs);
2) Change the client code to the following:
    b = new B();
    b.setTitle("some title");

    db.create(b);   // <- Add this line.................................

    a = new A();
    a.setB(b);
    db.create(a);





********************************************
Steve Ebersole
IT Integration Engineer
Vignette Corporation 
512.741.4195

Visit http://www.vignette.com

********************************************


-----Original Message-----
From: David Rault [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, March 05, 2002 11:32 AM
To: [EMAIL PROTECTED]
Subject: [castor-dev] simple relation


hi everybody

i used to 'play' with castor 0.8.11
and i'm currently trying to update my skills with newer versions (0.9.3.9,
0.9.3.13 and latest cvs)

my problem is a very simple relation that works with 0.8.11 does not work
with the previously named versions
actually it partly works, and that's my problem, i don't see why it behaves
that way :

i (quickly) searched the mailing-list archive but saw nothing matching...

i'v got a my generic super class for persistent objects : Super
i've got 2 subclasses : A and B
A has a field of type B

here is the code :

******************************
public class Super implements Persistent {
 private transient Database _database;
 private int id;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public Super() {
  super();
 }
 public void jdoPersistent(Database db) {
  setDatabase(db);
 }
 /*
 the other methods of Persistent are defined empty
 not shown here for lisibility
 */
 protected Database getDatabase() {
  return _database;
 }
 protected void setDatabase(Database database) {
  _database = database;
 }
 /** for backward compatibility with castor 0.8.11 */
 public void jdoCreate() throws Exception {
  jdoBeforeCreate(getDatabase());
 }
}
******************************
public class A extends Super {
 private B b;
 public A() {
  super();
 }
 public B getB() {
  return b;
 }
 public void setB(B b) {
  this.b = b;
 }
 public void jdoBeforeCreate(Database db) throws Exception {
  if (getB() != null) {
   if (getB().getId()==0) {
    db.create(getB());
   } else {
    db.update(getB());
   }
  }
 }
}
******************************
public class B extends Super {
 private String title;
 public B() {
  super();
 }
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
}
******************************

my mapping looks like this :
******************************
<?xml version="1.0"?>
<!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.exolab.org/mapping.dtd"; >
<mapping>
    <key-generator alias="mykeygen" name="MAX"></key-generator>
    <class access="shared" identity="id"
name="com.sysdeo.persistence.castor.test.Super" key-generator="mykeygen">
        <map-to table="super" xml="super"/>
        <field direct="false" lazy="false" name="id" required="true"
type="integer">
            <sql dirty="check" name="id" type="integer"/>
        </field>
    </class>
    <class access="shared" identity="id"
name="com.sysdeo.persistence.castor.test.A" key-generator="mykeygen"
extends="com.sysdeo.persistence.castor.test.Super">
        <map-to table="a" xml="a"/>
        <field direct="false" lazy="false" name="id" required="true"
type="integer">
            <sql dirty="check" name="id" type="integer"/>
        </field>
        <field name="b" direct="false" lazy="false" required="false"
type="com.sysdeo.persistence.castor.test.B">
            <sql dirty="ignore" name="bId" />
        </field>
    </class>
    <class access="shared" identity="id"
name="com.sysdeo.persistence.castor.test.B" key-generator="mykeygen"
extends="com.sysdeo.persistence.castor.test.Super">
        <map-to table="b" xml="b"/>
        <field direct="false" lazy="false" name="id" required="true"
type="integer">
            <description>l'identifiant de tout objet</description>
            <sql dirty="check" name="id" type="integer"/>
        </field>
        <field name="title" direct="false" lazy="false" required="false"
type="string">
            <description>le titre de l'objet</description>
            <sql dirty="check" name="title" type="varchar"/>
        </field>
    </class>
</mapping>
******************************

the id field is set by the key generator (here is a simple MAX algorithm but
i've got the same problem with
MySQL IDENTITY)

******************************
and finally here is what i try :

b = new B();
b.setTitle("some title");
a = new A();
a.setB(b);
db.create(a);
******************************

both 'a' and 'b' are persisted BUT the bId in 'a' is set to 0 in the
database
whereas the id of 'b' is set correctly in the B table

what have i missed ?

one constraint is that i don't want a reference to 'a' in 'b'
(only 'a' knows about 'b')
i've also tried to remove the common superclass (in case that would be some
locking for the id generation) : not better

thanks a lot for your help, i hope i gave enough details of my case

David

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to