I have a bidirectional mapping of a many-to-one relationship as listed
below. If I have the following code:
TestDatatypes blob = new TestDatatypes();
TestLookup lookup = new TestLookup();
Set<TestDatatypes> tdts = new HashSet<TestDatatypes>();
tdts.add(blob);
lookup.setTestDatatypes(tdts);
blob.setTestLookup(lookup);
dao.insert(lookup);
It works because the sql insert for the lookup table is ran first,
but if I
replace dao.insert(lookup) with dao. insert(blob), then the child
sql is
ran before the parent record has been inserted. as listed here:
INSERT INTO TEST_DATATYPES (TEST_DATATYPES_ID, MODIFY_DATE,
MODIFY_USER_ID,
COMMENTS, INSERT_DATETIME, INSERT_USER_ID, IS_ACTIVE, IS_TEST,
TEST_AMT,
TEST_DATATYPES_NAME, TEST_DATE, TEST_DATETIME, TEST_IND, TEST_TIME,
TEST_TIMEZONE, VER_ID, TEST_LOOKUP_ID) VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?) [params=(long) 359, (Timestamp) 2007-02-06
10:07:12.468, (long) 1, (null) null, (Timestamp) 2007-02-06
10:07:11.671,
(long) 34, (int) 1, (int) 0, (double) 0.0, (null) null, (null)
null, (null)
null, (null) null, (null) null, (null) null, (int) 1, (long) 40]
INSERT INTO I3.TEST_LOOKUP (TEST_LOOKUP_ID, MODIFY_DATE,
MODIFY_USER_ID,
COMMENTS, IS_ACTIVE, TEST_LOOKUP_ABBR, TEST_LOOKUP_ALT_NAME,
TEST_LOOKUP_CODE, TEST_LOOKUP_NAME, TEST_LOOKUP_SEQ, VER_ID) VALUES
(?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?) [params=(long) 40, (Timestamp) 2007-02-06
10:07:12.531, (long) 1, (String) my lookup, (int) 1, (String) Abbrev,
(String) alt name, (String) CODE, (null) null, (int) 0, (int) 1]
I believe this to be a bug as I have tried the same mappings with
other jpa
implementations and it works fine.
<entity class="TestDatatypes">
<table schema="I3" name="TEST_DATATYPES" />
<sequence-generator name="TestDataTypeSeq"
sequence-name="SEQ_TEST_DATATYPES_ID" allocation-
size="1"
/>
<attributes>
<id name="testDatatypesId">
<column name="TEST_DATATYPES_ID" />
<generated-value strategy="SEQUENCE"
generator="TestDataTypeSeq" />
</id>
...
<many-to-one name="testLookup" fetch="EAGER">
<join-column name="TEST_LOOKUP_ID" />
<cascade>
<cascade-all />
</cascade>
</many-to-one>
</attributes>
</entity>
<entity class="TestLookup">
<table schema="I3" name="TEST_LOOKUP" />
<sequence-generator name="LookupSeq"
sequence-name="SEQ_TEST_LOOKUP_ID" allocation-
size="1" />
<attributes>
<id name="testLookupId">
<column name="TEST_LOOKUP_ID" />
<generated-value strategy="SEQUENCE"
generator="LookupSeq" />
</id>
...
<one-to-many name="testDatatypes"
target-entity=
"com.ibm.ptp.i3.domain.test.TestDatatypes"
mapped-by="testLookup" fetch="EAGER">
<cascade>
<cascade-all />
</cascade>
</one-to-many>
</attributes>
</entity>