Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Mapping files should be in the omod/src/main/resources folder.  (or if not using maven, in /metadata folder)A hibernate mapping bi-directionally maps data from MySQL to Java and vice versa. A mapping might look something like this:

Code Block
languagexml
firstline1
titleExample Mapping File
linenumberstrue
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.openmrs.module.YourJavaClass">
    <class name="YourJavaClass" table="your_mysql_table">
        <id name="id" column="your_mysql_table_id">
            <generator class="native"/>
        </id>
        <property name="sampleInteger" type="java.lang.Integer" column="simple_integer"/>
        <property name="sampleString" type="java.lang.String" column="sample_string" length="255"/>
        <property name="uuid" type="java.lang.String" column="uuid" length="38" not-null="true" unique="true"/>
        <property name="dateCreated" type="java.util.Date" column="date_created" length="19" not-null="true"/>
        <many-to-one name="patient" class="org.openmrs.Patient" column="author_id" not-null="true"/>
        <one-to-one name="encounter" class="org.openmrs.Encounter" column="encounter_id" not-null="true"/>
		<one-to-many name="provider" class="org.openmrs.Provider" column="provider_id" not-null="true"/>
    </class>
</hibernate-mapping>


Creating an Object

  • Every table you create in the database requires its own hibernate mapping file in omod/src/main/resources
  • The name field must be the same name as your Java class
  • The column name must be the same as it is defined in your liquibase.xml file


 Common Fields

Fields
packageThe location of the java class file being mapped
class nameThe name of the java class being mapped
tableThe name of the MySQL table being mapped
idThe id of the object

column

the MySQL field relating to the defined variable


Common MySQL to Java translations

Fields
nameThe field defined by your java object
columnThe field defined by your MySQL table
typeThe data type of the data, integer, string, ect.
not-nullWhen set to be true the field is required when defining the object


Common MySQL relations

Relations are commonly translated from MySQL foreign keys to Java classes. 

Fields
Java class definition
one-to-oneYour object relates to one of the defined objectsDefined as the related object type
many-to-oneMultiple of your object can relate to one of another objectDefined as a list of the related object type

one-to-many

Your object can contain multiple of the related objectsDefined as a list of the related object type
classSimilar to type, this field defines the object type that is being related

Note: These associations are defined and created by your liquibase.xml file first, so you will need to reference those to determine the relation type.


There should be a mapping file for each entity that your module/hibernate is maintaining. If you have two objects, then make two classes called FormEntryQueue and FormEntryArchive, then make FormEntryQueue.hbm.xml and FormEntryArchive.hbm.xml and put them both in the resources folder.The config.xml file has an element called "mappingFiles" that lists off all hbm files that need to be loaded.  If


Deploying your mapping

If you are using the maven layout, this element is automatically populated with all found *.hbm.xml files.  If


If  using the pre-maven layout (ant) then you will need to list off each of your hbm.xml files in that element.

If you  are building using ant add the following lines to the bottom of your config.xml

Code Block
languagexml
titleconfig.xml
<mappingFiles>  
        YourObject.hbm.xml
</mappingFiles>