Hibernate Examples

OpenMRS currently uses Hibernate to connect the OpenMRS objects to the relational database tables.

You can see the hibernate mapping files in [source:openmrs/trunk/metadata/api/hibernate].

Modules must add hibernate.hbm.xml files to their /metadata file AND reference them in their config.xml files in the <mappingFiles> element.

[edit]

Example Java Object



package org.openmrs; import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.Locale; /** * ConceptName is the real world term used to express a Concept within the idiom of a particular * locale. */ public class ConceptName extends BaseOpenmrsObject implements Auditable { // Fields private Integer conceptNameId; private Concept concept; private String name; private Locale locale; private Collection<ConceptNameTag> tags; // Constructors /** default constructor */ public ConceptName() { } /** * Convenience constructor to create a ConceptName object by primary key * * @param conceptNameId */ public ConceptName(Integer conceptNameId) { this.conceptNameId = conceptNameId; } /** * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals(Object obj) { if (!(obj instanceof ConceptName)) { return false; } ConceptName rhs = (ConceptName) obj; if (this.conceptNameId != null && rhs.conceptNameId != null) return (this.conceptNameId.equals(rhs.conceptNameId)); else return this == obj; } /** * @see java.lang.Object#hashCode() */ public int hashCode() { if (this.getConceptNameId() == null) return super.hashCode(); int hash = 3; hash = hash + 31 * this.getConceptNameId(); return hash; } public Integer getConceptNameId() { return conceptNameId; } public void setConceptNameId(Integer conceptNameId) { this.conceptNameId = conceptNameId; } public Concept getConcept() { return concept; } public void setConcept(Concept concept) { this.concept = concept; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Locale getLocale() { return locale; } public void setLocale(Locale locale) { this.locale = locale; } public Collection<ConceptNameTag> getTags() { return tags; } public void setTags(Collection<ConceptNameTag> tags) { this.tags = tags; } }



[edit]

Hibernate Mapping File

This mapping file works with the above concept.

Some attributes defined here (like uuid, creator, voided, etc.) are defined on the parent class of ConceptName.

<generator />can also be the custom openmrs<generator />

<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.1//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="org.openmrs"> <class name="ConceptName" table="concept_name"> <id name="conceptNameId" type="int" column="concept_name_id"> <generator /> </id> <many-to-one name="concept" column="concept_id" class="Concept" not-null="true"/> <property name="name" type="java.lang.String" length="65535" not-null="true" /> <property name="locale" type="java.util.Locale" length="50" not-null="true"/> <property name="dateCreated" type="java.util.Date" column="date_created" not-null="true"/> <many-to-one name="creator" column="creator" not-null="true"/> <property name="voided" type="java.lang.Boolean" column="voided" length="1" not-null="true" /> <property name="dateVoided" type="java.util.Date" column="date_voided" length="19" /> <property name="voidReason" type="java.lang.String" column="void_reason" length="255" /> <many-to-one name="voidedBy" column="voided_by" /> <set name="tags" table="concept_name_tag_map" cascade="save-update"> <key column="concept_name_id"/> <many-to-many column="concept_name_tag_id"/> </set> <property name="uuid" type="java.lang.String" column="uuid" length="38" unique="true" /> </class> </hibernate-mapping>