Ok I changed the code as suggested by Andrus and I no longer get and
exception but when I look at the database PERSON.ADDRESS_ID is null so I am
back to my original problem. What's the correct method to set relationships?
Here is the new code.
import java.io.File;
import java.util.List;
import org.objectstyle.cayenne.access.*;
import org.objectstyle.cayenne.exp.*;
import org.objectstyle.cayenne.query.*;
import org.objectstyle.cayenne.conf.Configuration;
public class CayenneTst extends Object
{
public DataContext ctxt;
public CayenneTst()
{
initialize();
test1();
}
public void initialize()
{
Configuration conf = Configuration.getSharedConfig();
ctxt = conf.getDomain().createDataContext();
}
public void test1()
{
Address address =
(Address)ctxt.createAndRegisterNewObject("Address");
address.setCity("Austin");
address.setState("TX");
address.setStreetAddress1("2000 Research Blvd");
address.setZip("78759");
ctxt.commitChanges();
Person person =
(Person)ctxt.createAndRegisterNewObject("Person");
person.setFirstName("John");
person.setMiddleInitial("H");
person.setLastName("Cayenne");
person.setAddress(address);
ctxt.commitChanges();
}
public static void main(String[] args)
{
new CayenneTst();
}
}
Here is the person row:
MIDDLE_INITIAL FIRST_NAME ADDRESS_ID PERSON_ID LAST_NAME
H John 200 Cayenne
Here is the address row:
STREET_ADDRESS_2 CITY STREET_ADDRESS_1 ADDRESS_ID ZIP
STATE
Austin 2000 Research Blvd 260 78759 TX
J.R.
-----Original Message-----
From: Andrus [ <mailto:andru..bjectstyle.org>
mailto:andru..bjectstyle.org]
Sent: Monday, August 19, 2002 11:00 PM
To: JR Ruggentaler
Cc: cayenne-use..bjectstyle.org
Subject: Re: set method fails on relationship
At 12:40 AM 8/19/2002 -0500, you wrote:
>Address address = new Address();
> address.setCity("Austin");
> address.setState("TX");
> address.setStreetAddress1("2000 Research Blvd");
> address.setZip("78759");
> ctxt.registerNewObject(address, "Address");
The problem is that relationship is being set before the object is
registered with DataContext. While we should probably add a check for that
in "setReverseRelationship" method that throws the exception, I would
suggest creating address the following way in the first line of code:
Address address = (Address)ctxt.createAndRegisterNewObject("Address");
Andrus
This archive was generated by hypermail 2.0.0 : Tue Aug 20 2002 - 11:09:34 EDT