Hi Thomas,
When table A is defined, the primary key is not defined with an
autogenerated sequence.
CREATE TABLE A (
A_ID INTEGER NOT NULL,
...
Hence in repository.xml, I declare the PK for A as such:
<class-descriptor class="A" table="A">
<field-descriptor name="aId" column="A_ID" primarykey="true"
jdbc-type="INTEGER" />
However, for table B, the PK is defined with a sequence:
CREATE TABLE B (
B_ID INTEGER INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY( START WITH
1,
INCREMENT BY 1, NO CACHE),
...
Hence in repository.xml, I had to declare PK for B as such:
<class-descriptor class="B" table="B">
<field-descriptor name="bId" column="B_ID" primarykey="true"
jdbc-type="INTEGER" autoincrement="true" access="readonly" />
This setting works when I am inserting PKs for each of these tables.
However, I cannot directly set the reference IDs in table A. This code
doesn't work for me:
A a = new A();
a.setAId(aId);
a.setAName("aaa");
broker.clearCache();
broker.store(a);
B b = new B();
b.setXId(xId);
b.setYId(yId);
b.setBName("bbb");
broker.clearCache();
broker.store(b);
Integer bId = b.getBId();
a.setBId(bId);
a.setXId(xId);
a.setYId(yId);
broker.clearCache();
broker.store(a);
However, I was able to overcome this by directly setting the object
reference (instead of trying to set the foreign keys):
A a = new A();
a.setAId(aId);
a.setAName("aaa");
broker.clearCache();
broker.store(a);
B b = new B();
b.setXId(xId);
b.setYId(yId);
b.setBName("bbb");
broker.clearCache();
broker.store(b);
a.setB(b);
broker.clearCache();
broker.store(a);
This stores the FK references between A and B correctly. I don't know why
OJB does not like the FK field values set directly, but will create the
correct references when the object references are set.
Hope the reply wasn't too big. Thanks for your help.
Regards,
-Vamsi
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]