Hibernate - OR Mapping
Points to be covered
» Introduction to OR mapping
» Mapping Declaration
»<hibernate-mapping> element
» Mapping attributes
O-R-M
Let’s start with the introduction.
1. Introduction to OR mapping
Definition
» It is a programming technique for converting data between
incompatible type systems in object-oriented programming
languages.
» This creates, in effect, a "virtual object database" that can be used
from within the programming language.
» Compared to traditional techniques ORM often reduces the amount of
code that needs to be written.
O-R-M Introduction
O-R-M stands
for “object
relation
mapping”.
Mappings are
usually
defined in
XML
document.
Mappings are
constructed
around
persistent
class
declarations
XML
Guide to mapping
2.Mapping Declaration
Mapping Example
Mapping Document
<?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 package="org.somepackage.eg">
<class name="Foo" table=”FooTable”>
<id name="id" type=”java.lang.Long”>
<generator class=”sequence”/>
</id>
</class>
</hibernate-mapping>
7
XML
Root and attribute of schema
3.<hibernate-mapping> element
<hibernate-mapping> element
<hibernate-mapping
schema="schemaName"
catalog="catalogName"
default-cascade="cascade_style"
default-
access="field|property|ClassName"
default-lazy="true|false"
auto-import="true|false"
package="package.name"
/> Attributes are optional
The root element of hibernate
mapping document is <hibernate-
mapping> element.
XML
Elements & Attribute of <hibernate-mapping>
4.Mapping Attributes
<class> attributes
<class
name="ClassName"
table="tableName"
discriminator-
value="discriminator_value"
mutable="true|false"
schema="owner"
polymorphism="implicit|explicit"
where="arbitrary sql where
condition"
persister="PersisterClass" ...
/>
Attributes are optional
The <Class> element maps the domain
object with corresponding entity in the
database.
<id> attributes
<id
name="propertyName"
type="typename"
column="column_name"
unsaved-
value="null|undefined|id_value"
access="field|property|ClassName"
>
<generator
class="generatorClass"/>
/>
Attributes are optional
The <id> element defines the mapping
from that property to the primary key
column.
<property> attributes
<property
name="propertyName"
column="column_name"
type="typename"
update="true|false"
insert="true|false"
formula="arbitrary SQL
expression"
access="field|property|ClassName"
...
/> Attributes are optional
The <property> element declares a
persistent, JavaBean style property
of the class.
<generator> attributes
<id name="id" type="long" column="cat_id">
<generator class="org.hibernate.id.TableHiLoGenerator">
<param name="table">uid_table</param>
<param name="column">next_hi_value_column</param>
</generator>
</id>
The optional <generator> child element
names a Java class used to generate
unique identifiers for instances of the
persistent class.
Attributes are optional
<many-to-one> attributes
<many-to-one
name="propertyName"
column="column_name"
class="ClassName"
cascade="cascade_style"
fetch="join|select"
update="true|false"
insert="true|false"
...
/>
Attributes are optional
An ordinary association to another
persistent class is declared using a
many-to-one element.
<one-to-one> attributes
<one-to-one
name="propertyName"
class="ClassName"
cascade="cascade_style"
constrained="true|false"
fetch="join|select"
access="field|property|ClassName"
/>
Attributes are optional
A one-to-one association to another
persistent class is declared using a
one-to-one element.
Pros
»Portable
»Single language
»Nesting of data
»Adding is like modifying
»Slow
»Tuning
»Studying
»Complex Queries
cons
“
And I like asking questions, to keep
learning; people with big egos might not
want to look unsure.
» http://www.javatpoint.com/collection-mapping
» https://www.tutorialspoint.com/hibernate/hibe
rnate_or_mappings.htm
» https://en.wikipedia.org/wiki/Object-
relational_mapping
» http://www.allapplabs.com/hibernate/hibernat
e_mapping_in_depth.htm
Refrences
YOU For listening
THANK

Hibernate "O-R" Mapping

Editor's Notes

  • #5  For example, consider an address book entry that represents a single person along with zero or more phone numbers and zero or more addresses. This could be modeled in an object-oriented implementation by a "Person object" with attributes/fields to hold each data item that the entry comprises: the person's name, a list of phone numbers, and a list of addresses. The list of phone numbers would itself contain "PhoneNumber objects" and so on. The address-book entry is treated as a single object by the programming language (it can be referenced by a single variable containing a pointer to the object, for instance). Various methods can be associated with the object, such as a method to return the preferred phone number, the home address, and so on.
  • #10 The root element of hibernate mapping document is <hibernate-mapping> element. This element has several optional attributes. The schema and catalog attributes specify that tables referred to in this mapping belong to the named schema and/or catalog. If specified, tablenames will be qualified by the given schema and catalog names. If missing, tablenames will be unqualified. The default-cascade attribute specifies what cascade style should be assumed for properties and Collections which do not specify a cascade attribute. The auto-import attribute lets us use unqualified class names in the query language, by default.
  • #16 An ordinary association to another persistent class is declared using a many-to-one element. The relational model is a many-to-one association: a foreign key in one table is referencing the primary key column(s) of the target table.