Thursday 14 February 2013

Hibernate Mapping Explanation

Hibernate Mapping

<hibernate-mapping> </hibernate-mapping> The mapping document is an XML document having <hibernate-mapping> as the root element which contains two <class> elements corresponding to each class.

<class name="Employee" table="EMPLOYEE">
<class name="Employee" table="EMPLOYEE"> </class>  used to define specific mappings from a Java classes to the database tables.
name attribute of the class element
  table attributeof database table name
........................
</meta>
It's a optional .can be used to create class description
<id>
<id>
</id>
<id> element maps the unique ID attribute in class to the primary key of the database table.
name attribute used to id element refers to the property in the class.
column attribute used to id element refers to the column in the table.
type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type.
<generator>
<generator>
This element within id element.Hibernate generator element generates the primary key for new record.
It's used to generate the unique identifier for the object s of persistent class.
class attribute of generator element
class attribute of generator element
The following are the list of main generators we are using in the hibernate framework
column attribute used to id element refers to the column in the table.
  • all-delete-orphan


<meta attribute="class-description">
  • assigned
  • increment
  • sequence
  • identify
  • hilo
  • sqqhilo
  • native
  • foregin
  • uuid
  • guid
  • select
  • sequence -identity
assigned

  • This generator supports in all the databases
  • This is the default generator class used by the hibernate, if we do not specify <generator –> element under id element then hibernate by default assumes it as “assigned”

  • If generator class is assigned, then the programmer is responsible for assigning the primary key value to object which is going to save into the database

Increment

  • This generator supports in all the databases.
  • This generator is used for generating the id value for the new record by using the formulaMax of id value in Database + 1.
  • if we manually assigned the value for primary key for an object, then hibernate doesn’t considers that value.
Sequence:
  • Not support mySql
  • Database is dependent.we cannot use all database.we should know whether support or not.
  • While inserting new record in a database ,hibenate get next value from the sequence under assigns that value for the new record.
  • The programmer has created a sequence int database the name shoud passed as the generator

<id name=”employeeId” column=”empid”>
<generator>
<param name=”sequence”>mysequence</param>
</generator>
</id>
Note:above example we set mysequence value 2 new record inserted into second row
  • If the programmer has not passed any sequence name, then hibernate creates its own sequence with name “Hibernate-Sequence” and gets next value from that sequence, and than assigns that id value for new record.
  • If hibernate want’s to create its own sequence, in hibernate configuration file,hbm2ddl.auto property must be set enabled

identity
  • Database is dependent,not working in Oracle
  • identity generator is similar to increment generator, but the difference was increment generator is database independent and hibernate uses a select operation for selecting max of id before inserting new record
  • But in case of identity, no select operation will be generated in order to insert an id value for new record by the hibernate
hilo
  • Databse independent.
  • First record inserted id value as 1, for second record 32768,or the next records the id value will be incremented by 32768 and will stores into the database (i mean adds to the previous).
  • This hibernate stores the count of id values generated in a column of separated table, with name “hibernate_unique_key” by default with the column name “next_hi”.

native

  • when we use this generator class, it first checks whether the database supports identify or not, if not checks for sequence and if not, then hilo will be used finally the order will be..
    • sequence
    • identify
    • hilo
forign
  • It uses the id of another associated object,mostly use with ono-to-one relationship.
<property>
</property>
The property element used to map the class into database table .
name attribute used to id element refers to the property in the class.
type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type.
<set>

<set> element is new here and has been introduced to set the relationship between two class.

cascade

  • Main concept of hibernate relations is to getting the relation between parent and child class objects Cascade attribute is mandatory, when ever we apply relationship between objects, cascade attribute transfers operations done on one object onto its related child objects
Cascade having the values…….
  • none (default)
  • save
  • update
  • save-update
  • delete
  • all
if we write cascade = “all” then all operations like insert, delete, update at parent object will be effected to child object also
Example: if we apply insert(or update or delete) operation on parent class object, then child class objects will also be stored into the database.
all-delete-orphan means, breaking relation between objects not deleting the objects from the database,if a child record is removed from the collection and if we want to remove that child record immediately from the database, then we need to set the cascade =”all-delete-orphan”.

name:
name attribute is set to the defined Set variable in the parent class.
Key:
holds the foreign key to the parent object 



No comments:

Post a Comment