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 Bonnie MacKellar <BM...@mobius.com> on 2003/05/21 21:52:31 UTC

a persistent class question

I'm using the PersistenceBroker API and
I've been following the coding style in the tutorials.
One thing that looks strange to me is that, when
I have a class which has a reference to another
class, I end up with both a reference field and
a foreign key field. For example, a User object
refers to a Customer object :

public class User implements InterfaceUser{
	
	private int userID;
	protected String loginID;
	protected String fname;
	protected String mname;
	protected String lname;
	protected String passwordhash;
	protected Collection roles;
	protected InterfaceCustomer customer;
	protected int customerID;

Note that I have both customer and customerID fields in here.
Is this really how I should do it? And is it sufficient then
to just set the customer for a user object :
    User user = new User();
   // set various attributes
  // find a customer and add it to the user
  InterfaceCustomer custToAdd = (InterfaceCustomer)
broker.getObjectByQuery(query2);
  user.setCustomer(custToAdd);
  broker.store(user);

or, do I also need to explicity set the customer id :
   user.setCustomer(custToAdd);
   user.setCustomerID(custToAdd.getCustomerID());

I am confused because having both customer and customerID fields seems
redundant to me.

thanks,
Bonnie MacKellar
software engineer
Mobius Management Systems, Inc.
bmackell@mobius.com


Re: a persistent class question

Posted by David Warnock <da...@sundayta.com>.
Bonnie,

> I'm using the PersistenceBroker API and
> I've been following the coding style in the tutorials.
> One thing that looks strange to me is that, when
> I have a class which has a reference to another
> class, I end up with both a reference field and
> a foreign key field. For example, a User object
> refers to a Customer object :

You are doing this correctly ie set the object not the id.

In the latest cvs (possibly in RC3) there is an anonymous howto which 
tells you how to avoid this.

I tend to not provide set/get for the id field.

Dave
-- 
David Warnock, Sundayta Ltd. http://www.sundayta.com
iDocSys for Document Management. VisibleResults for Fundraising.
Development and Hosting of Web Applications and Sites.