gopal wrote:
I have a database table which dont have a primary key .It has only foreign key.
so how to define a xml mapping file for that table.
I tried giving foreign key in <id> tag. but when the query executes it returns only one row, eventhough if there are more row exists.
Who is managing the constraint ? Hibernate or the application ? I have a relationship like that where my application manages the constraint so I use an "assigned" id.
Code:
<hibernate-mapping>
<class name="com.foobar.data.MajorObject" table="major_object">
<id name="id" type="long" column="id">
<generator class="native" />
</id>
<property ... />
</class>
<class name="com.foobar.data.MinorObject" table="minor_object">
<id name="objectId" type="long" column="object_id">
<generator class="assigned" />
</id>
<property ... />
</class>
</hibernate-mapping>
So the creation code looks like:
Code:
Session hsession = HibernateSesson.currentSession();
MasterObject maxo = new MasterObject();
maxo.setFoobar(0);
hsession.save(maxo);
// Now I have the foriegn key to work with to persist MinorObject instance
MinorObject mino = new MinorObject();
mino.setObjectId(maxo.getId());
mino.setFoobar(0);
hsession.save(mino);
Now I guess it is possible to have mappings of <many-to-one ..> or <one-to-one ...> in MajorObject to allow a field to be linked and lazy loaded, But in my application I don't need that, in my application the MinorObject optionaly exists for each MajorObject and the graph traversal from MajorObject to MinorObeject does not happen in app code. The MinorObject are independantly managed with their own lifecycle by a separate task.
Don't forget to rate... Don't forget to help others... Even if you dont know the answer, you might learn thing new, I know I do :)