Вы находитесь на странице: 1из 19

Hibernate Session

Sachin Misurkar
Pradeep Awachar
Agenda :
• What is hibernate ???
• Architecture
• O-R Mapping

• Types of Mapping
-One to one
-Many to many
-One to many
-Many to one

• Inheritance Mapping
- Table per class hierarchy
- Table per subclass
- Table per concrete class
Hibernate
• What is Hibernate ???
• Open Source persistence technology
• Solution for object relational mapping and a persistence
management.
Architecture
• High Level Architecture
Continued….

• Architecture component

1 - Connection management
2 - Transaction management
3 - O-R Mapping
OR Mapping
• Mapping the Object with DB table or vice versa
• Basic Structure of Hibernate mapping
<?xml version="1.0"?>
<hibernate-mapping> -----------------------------------------> 1
<class name="Cat" table="CATS“> ------------------------->2
<id name="id" column="uid" type="long"> --------------- >3
<generator class=“assigned"/> ------------------------->4
</id>
<property name="sex" not-null="true" />--------- > 5

<!– Any Relation mapping one-one ,one – many etc… 


</class>
</hibernate-mapping>
Basic Structure of Hibernate mapping

• Hibernate Mapping

<hibernate-mapping
schema="schemaName"
default-cascade="none|save-update"
auto-import="true|false"
package="package.name"
/>
Basic Structure of Hibernate mapping

• Class
<class
name="ClassName"
table="tableName"
discriminator-value="discriminator_value"
mutable="true|false"
schema="owner"
dynamic-update="true|false"
dynamic-insert="true|false"
select-before-update="true|false"
where="arbitrary sql where condition"
persister="PersisterClass"
batch-size="N"
optimistic-lock="none|version|dirty|all"
lazy="true|false"
/>
Basic Structure of Hibernate mapping

• ID

<id
name="propertyName" (1)
type="typename" (2)
column="column_name" (3)
unsaved-value="any|none|null|id_value" (4)
access="field|property|ClassName"> (5)
<generator class="generatorClass"/>
</id>
One to One Hibernate Mapping :

• One to One :
<one-to-one
name="propertyName"
class="ClassName"
cascade="all|none|save-update|delete"
constrained="true|false"
access="field|property|ClassName"
/>

Eg. Author and Book

<one-to-one name="author" class="test.hibernate.Author" constrained="true"


cascade="all"/>
<one-to-one name="book" class="test.hibernate.Book" cascade="all"/>
Many to Many Hibernate Mapping

Eg : Person and Address

<set name="addresses" table="Person_Address" cascade="all">


<key column="PERSON_ID" />
<many-to-many column="ADDRESSS_ID" class="test.hibernate.Address" />
</set>
Hibernate
Basic operation
Select, insert, update, delete
Association Mappings
• One to many Mapping
• Many to one Mapping
Inheritance Mapping
• Table per class hierarchy
• Table per subclass
• Table per concrete class
Association Mappings
Dept
• dpId, dpName, location,dailyProd.
Employee
• empId, empName, deptId, salary.
Dept.dpID = Employee.deptId
• One to many Mapping – One dept Id
having many employees
• Many to one Mapping – Many Employees
have one dept Id
Inheritance Mapping

Hibernate supports the three basic


inheritance mapping strategies

• Table per class hierarchy


• Table per subclass
• Table per concrete class
Inheritance Mapping

• It is possible to use different mapping


strategies for different branches of the same
inheritance hierarchy.
• <subclass>, <joined-subclass>, <union-
subclass>
• Hibernate does not support mixing <subclass>,
and <joined-subclass> and <union-subclass>
mappings under the same root <class> element.
• Since Hib3 ordering is does not matter first
Super then Sub classes mapping
Inheritance Mapping
Table per class hierarchy

• Exactly one table is required for the all


hierarchy. Means all for super and
subclasses there is one table .
• There is one limitation for this strategy
that columns declared by the subclasses
may not have NOT NULL constraints
Inheritance Mapping
Table per subclass
• In this strategy for each super class and sub
class there is one separate table required
• In this type important thing is all sub class must
have primary key association with the super
class.
• <joined-subclass> and < subclass >can be used
• we can map using discriminator( who
differentiates ) tag with < subclass > -- declares
a discriminator column of the table.
Inheritance Mapping
Table per concrete class
• In this type for every subclass there is one
table but not for super class.
• Each tables defines columns for all
properties of the class including inherited
properties.
• Limitation – if property is mapped on the
super class the column name must be the
same on all subclass table.
Inheritance Mapping
Difference between Table per subclass
and Table per concrete class
• 1. Parent class mapping must be defined
primary key association must be defined in
each sub class [ Table per subclass ]
• 2.Parent class mapping is not there but
parent class attributes are present in each
sub class. [ Table per concrete class ]
instead of primary key

Вам также может понравиться