You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Jeffrey Gilbert <vi...@attbi.com> on 2003/03/03 02:27:45 UTC

What am I doing wrong? Not able to persist a 1:M relationship. SourceCode included...

I have the following two class files:

package com.gilbert;
import java.util.Collection;
public class Applications {
    private int _id;
    private String name;
    private String description;
    private Collection events;  // To hold many event objects

    public void set_id(int _id) {  this._id = _id;  }
    public int get_id() { return _id;  }
    public String getName() { return name;  }
    public void setName(String name) { this.name = name; }
    public String getDescription() { return description; }
    public void setDescription(String description) { 
      this.description=description;
    }
    public Collection getEvents() { return events; }
    public void setEvents(Collection events) { events = events; }

    public Applications() {
    }
}

package com.gilbert;
public class Event {
    protected int _id;
    protected event;
    protected int application_id;

    public void set_id(int _id) { this._id = _id; }
    public int get_id() { return _id; }
    public String getEvent() { return Event; }
    public void setEvent(String event) { this.event = event; }
    public int getApplication_id() { return application_id; }
    public void setApplication_id(int application_id) {
        this.application_id = application_id;
    }

    public Event() {
    }
}

I have the following user defined mappings:

   <class-descriptor
        class="com.gilbert.Event"
        table="Event">
      <field-descriptor
         name="_id"
         column="ID"
         jdbc-type="INTEGER"
         primarykey="true"
         autoincrement="true"
      />
      <field-descriptor
         name="event"
         column="EVENT"
         jdbc-type="VARCHAR"
      />
      <field-descriptor
         name="application_id"
         column="APPLICATION_ID"
         jdbc-type="INTEGER"
         indexed="true"
      />
      </reference-descriptor>
    </class-descriptor>

    <class-descriptor 
         class="com.gilbert.Applications" 
         table="Applications">
    <field-descriptor 
         name="_id" 
         column="ID" 
         jdbc-type="INTEGER"
         primarykey="true"
         autoincrement="true"
      />
      <field-descriptor
         name="name"
         column="NAME"
         jdbc-type="VARCHAR"
      />
      <field-descriptor
         name="description"
         column="DESCRIPTION"
         jdbc-type="VARCHAR"
      />
       <collection-descriptor
         name="events"
         element-class-ref="com.gilbert.Event"
         orderby="_id"
         sort="DESC">
       <inverse-foreignkey field-ref="application_id"/>
       </collection-descriptor>
    </class-descriptor>

Now I use the following code in my program but nothing seems to persist.

	//Create new application object.
	Application application = new Application();
	application.setName("test1");
	application.setDescription("This is a test");
	
	//Create new event object.
            Event newEvent = new Event();
            newEvent.setEvent("test123");
	
	//Load new event object in vector
	Vector e = new Vector();
            e.add(newEvent);

	//Add Vector that contains event into application object.
	application.setEvents(e);
            
            // now perform persistence operations
            try  {
                broker.beginTransaction();
                broker.store(application);
                broker.commitTransaction();
            }
            catch (PersistenceBrokerException pbe)
            {
                broker.abortTransaction();
            }

What am I doing wrong?  Do I need to persist the event object before I 
persist the application object?  Is my user defined mapping file okay?

Thanks in advance,

Jeff