Hibernate version:2.1.6
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using:Oracle 9i
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
I have 2 tables , one is the parent with a string primary key and the other is child with a composite primary key. The child's composite key contains a referenced foreign key which is the primary key of the parent. the two mapping files are like this :
Parent Primary Key
<id
name="testId"
type="java.lang.String"
column="TEST_ID"
length="20"
>
<generator class="assigned" />
</id>
Child Composite Key
<composite-id name="comp_id" class="child">
<key-property
name="createDt"
column="CREATE_DT"
type="java.sql.Timestamp"
length="7"
/>
<!-- bi-directional many-to-one association to Parent-->
<key-many-to-one
name="testId"
class="Parent"
>
<column name="TEST_ID" />
</key-many-to-one>
</composite-id>
I created a parent class and a child class, set all the values for both classes. And tried to do the following:
HashSet hs = new HashSet();
Parent parent = new Parent();
parent.setTestId("test");
Child child = new Child();
child.setCompId("child",parent);
hs.add(child);
parent.setChild(hs);
Session ses = SessionFactory.openSession();
Transaction tx = ses.beginTransaction();
session.save(parent);
tx.commit();
From the code above I'm trying to create a parent record and a child record at one time by storing the created Child class into a hashset which in turn stores into the set method inside the parent class. But however only the parent record is created and the child record is not created. I got the message from the log file Collection found: [Child#330], was: [<unreferenced>]; but i do not know what it means. But however i can get round by the following codes;
session.save(parent);
session.save(child);
But I want to know is there anyway for me just to do a save on parent class (with the child class stored inside the class) rather than to do 2 save methods.
Would appreciate if anyone could help. Feel free to comment if what i did is wrong.
|