salaam, imagine we have a table called "main" in a database and it has a foreign key "fkey" from another table called "foreign". we want to make classes and mapping files for them:
classes:
class MainClass { private int id; //primary key private int fkey; // foreign key private ForeignClass fClass; //foreign table's class
public MainClass() { }
// ... id, fkey and fClass setters and getters }
class ForeignClass {
private int id; //primary key private String name; //a sample column public ForeignClass() { }
// .... id and name setters and getters }
mapping files (.hbm.xml): MainClass: <hibernate-mapping> <class name="MainClass" table="main" lazy="false" > <id name="id" column="mId" > <generator class="assigned" > </id> <property name="fkey" column="mfkey" type="int" /> <many-to-one name="fClass" class="ForeignClass " column="fkey" insert="false" update="false" /> </class> <query name="testQuery">select test from MainClass as test left join test.fClass where test.id = 11 </query> </hibernate-mapping>
ForeignClass :
<hibernate-mapping> <class name="ForeignClass " table="foreign" lazy="false" > <id name="id" column="mId" > <generator class="assigned" > </id> <property name="name" column="fname" type=" </class> </hibernate-mapping>
in your hibernate.cfg.xml file you need to add the address of both of this mapping files:
<mapping resource="src/main.hbm.xml" /> <mapping resource="src/foreign.hbm.xml" />
now to implement them you need to make an instance of MainClass for example: MainClass mainClass;
then retreive the data in it. then you can have the value of foreign table with mainClass.getForeginClass()... I didn't describe the way to implement it in the end because you can implement it in many ways. I hope this will help. also I should remind you that this kind of mapping is only for tables which have many to one relations, I mean many to one relations.
have a good time, Hooman :)
_________________ Hooman Peiro Sajjad
|