Tasklist Hibernate 3 HBM file
- Official basic documentation on the HBM file format for Hibernate 3
We will be creating a basic hibernate HBM file which is hibernate 3 compatible, you should use the hibernate tools for eclipse but the instructions below will show how to write the file by hand
- Create a java package to hold the hibernate hbm file
- Right click on the java src folder (tool/src/java) and select New -> Package
- Note: sometimes a new source folder is created for hibernate files (e.g. tool/src/hibernate)
- For the name enter: org.sakaiproject.tool.tasklist.hibernate
- Right click on the java src folder (tool/src/java) and select New -> Package
- Create a new file by right clicking on the package and selecting New -> File
- Enter the name for the file as: Task.hbm.xml
- Start with this simple HBM file template and paste this text into the new file:
<?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="org.sakaiproject.tool.MyTool.MyClassImpl" table="MYTOOL_MYCLASS"> <id name="id" type="long" column="MYCLASS_ID"> <generator class="native"> <param name="sequence">MYCLASS_ID_SEQ</param> </generator> </id> <property name="myProperty" type="string" length="255" not-null="true"> <column name="MY_PROPERTY"/> </property> </class> </hibernate-mapping>
- We want to create an HBM which will create a mapping between the Task and the database
- Change the class name to org.sakaiproject.tool.tasklist.impl.TaskImpl
- Change the class table to TASKLIST_TASKS
- Change the id column to TASK_ID (optional, but do not leave as the template default)
- Change the id generator param to TASKLIST_TASK_ID_SEQ (optional, but must be unique)
- Next we need to add in the 4 properties from the Task object
We will use the hibernate native types in this example- owner - the creator of the task
- siteId - the site this task was created in
- creationDate - when was the task created
- task - the text of the task
- Create the property mapping for the owner property
<property name="owner" type="string" length="255" not-null="true"> <column name="TASK_OWNER"/> </property>
- This will contain the userId string (which will be a big long number with dashes in it)
- type - hibernate native string
- length - 255 chars
- cannot be null
- Create the property mapping for the siteId property
<property name="siteId" type="string" length="255" not-null="true"> <column name="TASK_SITE_ID"/> </property>
- This will contain the siteId string (e.g. mercury)
- type - hibernate native string
- length - 255 chars
- cannot be null
- Create the property mapping for the creationDate property
<property name="creationDate" type="timestamp" not-null="true"> <column name="TASK_CREATION_DATE"/> </property>
- This will contain the date and time the task object was created
- type - hibernate native timestamp (date and time)
- cannot be null
- Create the property mapping for the task property
<property name="task" type="text" not-null="true"> <column name="TASK_TEXT"/> </property>
- This will contain the text of the task itself (could be any length)
- type - hibernate native text (unlimited string)
- cannot be null
- HBM file creation complete
- The HBM file is available here: Task.hbm.xml