Now that the Objects have been created, it is time to match them to Relational aspect of the application or the database tables.
We have an option of creating the database manually through SQL and then associating them with Objects or Classes created. Or we can do it the other way round.
In this case we proceed by first creating our Object world. Hibernate already provides users with the tools to generate the SQL scripts necessary for this purpose. We shall use the same. But the first step would be to create the mappings for Database related tables, columns, constraints etc that would be used.
This can be achieved either by mapping files or via annotations. I shall prefer the longer (but my personal favorite) hbm files for achieving this.
Owner.hbm.xml
We have an option of creating the database manually through SQL and then associating them with Objects or Classes created. Or we can do it the other way round.
In this case we proceed by first creating our Object world. Hibernate already provides users with the tools to generate the SQL scripts necessary for this purpose. We shall use the same. But the first step would be to create the mappings for Database related tables, columns, constraints etc that would be used.
This can be achieved either by mapping files or via annotations. I shall prefer the longer (but my personal favorite) hbm files for achieving this.
Owner.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.menagerie.domain.entity.Owner" table="OWNER" > <id name="id" type="long" access="property" unsaved-value="null"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="string" index="" length="100" not-null="true" unique="false" > <column name="NAME" /> </property> <property name="userId" type="string" length="120" not-null="true" unique="true" > <column name="USERID" /> </property> <set name="pets" table="PET" inverse = "true" lazy="true" batch-size="5" cascade="all-delete-orphan" > <key> <column name="OWNER_ID" /> </key> <one-to-many class="com.menagerie.domain.entity.Pet" /> </set> <property name="modifiedBy" type="long"> <column name="MODIFIED_BY" /> </property> <property name="createdBy" type="long"> <column name="CREATED_BY" /> </property> <property name="createdDate" type="timestamp"> <column name="CREATED_DATE" /> </property> <property name="modifedDate" type="timestamp"> <column name="MODIFIED_DATE" /> </property> </class> </hibernate-mapping>
Pet.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.menagerie.domain.entity.Pet" table="PET"> <id name="id" type="long"> <column name="ID" /> <generator class="native" /> </id> <property name="name" type="string"> <column name="NAME" /> </property> <property name="age" type="integer"> <column name="AGE" /> </property> <property name="tagId" type="string"> <column name="TAGID" /> </property> <many-to-one name="owner" class="com.menagerie.domain.entity.Owner" fetch="join"> <column name="OWNER_ID" /> </many-to-one> <property name="modifiedBy" type="long"> <column name="MODIFIED_BY" /> </property> <property name="createdBy" type="long"> <column name="CREATED_BY" /> </property> <property name="createdDate" type="timestamp"> <column name="CREATED_DATE" /> </property> <property name="modifedDate" type="timestamp"> <column name="MODIFIED_DATE" /> </property> </class> </hibernate-mapping>
Certain Points of Note:
- It is not necessary to have a separate mapping file for every mode class. This however leads to good readability. An alternative definition would be:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.menagerie.domain.entity.Owner" table="OWNER" > </class> <class name="com.menagerie.domain.entity.Pet" table="PET" > </class> </hibernate-mapping>
- The <hibernate-mapping> and other xml elements used in the file supports several attributes,sql queries to create/retrieve/update/delete our data. However only that were deemed necessary or needed in the upcoming code were used.
- The hbm files thus created need to be available on the project class path for correct execution.
No comments:
Post a Comment