Introduction to Hibernate Mapping
There are different relations that we maintain to establish a link between different database tables in relational database models. These relations are one to one, one to many, and many to many. A similar concept is being installed in hibernate. Here the hibernate works to link the JAVA language to the database table along with this link we can establish relations/mappings. These mappings can be used to navigate through the database. This mapping is defined in the XML sheet. This is generally written by coders but different tools can also be used to create it. Some of these tools are XDoclet, AndroMDA, and Middlegen.
Primary Types of Hibernate Mapping
There are primarily three types of mapping. These are:
- One to one: In this kind of relationship one attribute is mapped to another attribute in a manner such that only one-to-one mapping is maintained. This can be better understood with the help of an example. For example, If one person works for one department only. The same person can not be employed by another department then that mapping is called one to one.
- One to many: In this kind of relationship one attribute is mapped to another attribute in a manner such that one attribute is mapped to many other attributes. This can be better understood with the help of an example. for ex: If one student is a member of different groups. Like a cultural group, sports club, robotics club at the same time. In that case, the student and group relationship is called many to one relationship.
- Many to many: In this kind of relationship one attribute is mapped to another attribute in a manner such that any number of attributes can be linked to other attributes with no restriction on the number. This can be better understood with the help of an example. for example, In the library, one person can take multiple books and also one book can be issued to multiple books. This kind of relationship is called many to many relationships. This is a complex relationship and needs a lot of understanding of the business use case before implementation.
Hibernate Mapping Detailed Explanation
If we go through the code then we understand that there is a table EMP_ATTR which is created in the database to store employee attributes that have columns like first name, last name, and salary. The data from java application is stored in this table which is developed at the front end.
Technical specification based on the code written to explain:
<hibernate-mapping> is a root node which contains <class> elements in it. Class is used to link java with the database via two attributes. Class name “emp” is the class name taken from java code while the table” EMP_ATTR” is the table name from the database. <id> element help in mapping the primary key to unique IDs.
The primary key is present in the database while unique IDs are derived from a java class. <id> name is coming from java while the column is the column from a table in the database. <id> type attribute have the hibernate mapping style which converts java data type to sql data type. <generator> class is used to generate the primary key automatically. The generator element is “native”.
This gives an indication to hibernate that it can pick any designed algorithm like Hilo, identity or sequence algorithm to create a primary key. Finally, a <property> class. This is the defining class that maps java class property to the column in the table of the database. The name attribute refers to java class property name while the column is the column from a table in the database. The type attribute holds the hibernate type which will help the system determine the data type when java class data is converted into RDBMS( Relational database management system) data type.
This is how the XML file looks like. It is sourced from hibernate.org which is the official website of Hibernate.
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
<class name = "EMP" table = "EMP_ATTR">
<id name = "id" type = "int" column = "id">
<property name = "fName" column = "firstName" type = "string"/>
<property name = "lName" column = "lastName" type = "string"/>
<property name = "AnnualSalary" column = "AnnualSalary" type = "int"/>
This file is saved in the format <classname>.hbm.xml. In this case, the file should have been stored on the name EMP_ATR.hbm.xml.
Hibernate Mapping Type
So in the previous code example, we see the hibernate mapping types in the XML file. These mapping types can be of many types:
- Primitive: These type of mapping have data types defined as “integer”, “character”, “float”, “string”, “double”, “Boolean”, “short”, “long” etc. These are present in hibernate framework to map java data type to RDBMS data type.
- Date and Time: These are “date”, “time”, “calendar”, “timestamp” etc. Like primitive we have these date and time datatype mappings.
- Binary and large objects: These types are “clob”, “blob”, “binary”, “text” etc, Clob and blob data types are present to maintain the data type mapping of large objects like image and videos.
- JDK linked: Some of the mappings for objects which lie beyond the reach of the previous type of mappings are included in this category. These are “class”, “locale”, “currency”, “timezone”.
Hence hibernate mapping is a concept that can be realized by establishing the mappings using XML files. These mappings become the base of a database designed as per business model requirements. This helps in determining the relationships between the persistent objects in the database. This mapping is critical for database design as it becomes the base for the front-end application in terms of performance, accuracy, and speed.
This is a guide to Hibernate Mapping. Here we discuss hibernate mapping with a detailed explanation, types and the primary types of Hibernate Mapping along with the sample code. You may also look at the following article to learn more –