Hi Peter,
as I understand your problem you need the Notes class according to some other
design issues even if castor do not need it.
As you stated in the subject of your mail you should use Cards class as a wrapper
for the Notes class. With this design you need all methods castor wants to access
for Notes in the Cards class. As with this design castor do not need to know about
Notes you do not need a mapping for Notes.
Lets start without the Notes class:
<mapping>
<class name="Card" identity="id">
<map-to table="cards"/>
<field name="id" type="String">
<sql name="cardid"/>
</field>
<field name="notes" type="Note" collection="vector"
get-methode="getAllNotes" add-methode="addNote">
<sql many-key="cardid"/>
</field>
...
</class>
<class name="Note" identity="id">
<map-to table="notes"/>
<field name="id" type="integer"/>
<sql name="id"/>
</field>
<field name="card" type="String"/>
<sql name="cardid"/>
</field>
<field name="note" type="string">
<sql name="text"/>
</field>
...
</class>
</mapping>
//CARD
public class Card
implements java.io.Serializable {
private String id;
public String getId() { return this._id; }
public void setId(String id) { this._id = id; }
private Vector _notes = new Vector();
public Vector getAllNotes() { return this._notes; }
public void addNote(Note note) {
this._notes.add(note);
note.setCard(this);
}
...
}
//NOTE
public class Note
implements java.io.Serializable {
private int _id;
public int getId() {return this._id;}
public void setId(int id) {this._id = id;}
private Card _card;
public Card getCard() {return this._card;}
public void setCard(Card card) {this._card = card;}
private String _note;
public String getNote() {return this._note;}
public void setNote(String note) {this._note = note;}
...
}
When we want to implement the Notes class like:
// NOTES
public class Notes
implements java.io.Serializable {
private Vector _notes = new Vector();
public Vector getNotes() { return this._notes; }
public void addNote(Note note) {
this._notes.add(note);
note.setCard(this);
}
}
We have to change the Card class like:
//CARD
public class Card
implements java.io.Serializable {
private String id;
public String getId() { return this._id; }
public void setId(String id) { this._id = id; }
private Notes _notes = new Notes();
// getAllNotes() used by castor to get vector of Note objects
public Vector getAllNotes() { return this._notes.getNotes(); }
// addNote() used by castor to add a Note object
public void addNote(Note note) { this._notes.addNote(node); }
// additional methodes for accessing the wrapped Notes object
// by other application code
public Notes getNotes() { return this._notes; }
// be careful with setNotes() not to break bi-directional mapping
public void setNotes(Notes notes) {
// remove all Note objects from this._notes
// add all Node objects from notes
}
...
}
I am not sure if this will work, but according to my basic understanding of
castor it should.
Ralf
----- Original Message -----
From: "McEvoy, Peter" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, March 13, 2003 12:42 PM
Subject: [castor-dev] How do I use a wrapper class correctly? Getting
MappingException: Field element ... not found!
> Hi all,
> I've been using Castor for a few days now, and think it's fantastic - why haven't I
> been using O-R/XML tools before?!
>
> However, I'm starting to do some more complex mapping and really getting stumped. I
> want to go from an RDBMS schema, to an Object
Model, and then marshall to XML
>
> I have the following RDBMS table schema:
>
> +-------------------+ +----------------+
> | CARDS | | NOTES |
> +-------------------+ +----------------+
> | cardid(#) VARCHAR |<----| cardid VARCHAR |
> | ... | | id(#) NUMBER |
> +-------------------+ | text VARCHAR |
> +----------------+
>
> (This is a one-to-many mapping - each entry in the CARDS table can have many NOTEs
> in the NOTES table
>
>
> I have the following Beans (! Below, I have removed all my package naming so as to
> increase readability !)
>
> //CARD
>
> public class Card
> implements java.io.Serializable {
> ...
> private String id;
> public String getId() { return this._id; }
> public void setId(String id) { this._id = id; }
>
> private Notes _notes = new Notes();
> public Notes getNotes() { return this._notes; }
> public void setNotes(Notes notes) { this._notes = notes; }
> ...
> }
>
> // NOTES
> public class Notes
> implements java.io.Serializable {
> private String _cardid;
> public String getId() { return this._cardid; }
> public void setId(String id) { this._cardid = id; }
>
> private Vector _notes = new Vector();
> public Vector getNotes() { return this._notes; }
> public void addNote(Note note) { _notes.add(note); }
> }
>
> //NOTE
> public class Note
> implements java.io.Serializable {
> private String _note;
> private int _id;
> private String _cardid;
>
> public String getNote() {return this._note;}
> public void setNote(String note) {this._note = note;}
> public int getId() {return this._id;}
> public void setId(int id) {this._id = id;}
> public String getCardid() {return this._cardid;}
> public void setCardid(String cardid) {this._cardid = cardid;}
> }
>
> Basically, I want to wrap alll the note elements into a collection in a Notes object
>
>
> Now I am using the following mapping.xml:
>
>
> <mapping>
> <class name="Card" identity="id">
> <map-to table="cards"/>
> ...
> <field name="notes" type="Notes"/>
> </class>
>
> <class name="Notes" identity="id">
> <field name="id" type="string"/>
> <field name="notes" type="Note" collection="vector">
> <sql many-key="cardid"/>
> </field>
> </class>
>
> <class name="Note" identity="id">
> <map-to table="notes"/>
> <field name="id" type="integer"/>
> ...
> <field name="note" type="string">
> <sql name="text"/>
> </field>
> </class>
> </mapping>
>
>
> Now you can see that the Notes object does not have a corresponding table that it
> maps to - it's just a wrapper for the
collection. How does the cardid get passed from the Card mapping to the Notes mapping
and thus onto the Note mapping? Is the
mapping correct?
>
> Regardless, it seems that no matter what I do to the Notes mapping above, I get the
> following error message:
>
> org.exolab.castor.mapping.MappingException: Field element, "Notes" not found!
>
> Where is this going wrong?
> Am I approaching this wrapping correctly?
>
>
> Any feedback would be appreciated (and thanks for reading this far if you've got
> this far!)
>
> Pete
>
> -----------------------------------------------------------
> 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