Elements of the Hibernate mapping files
1. Class: It specifies how a POJO class is mapped to a database table. The class element's name attribute is used to specify the Java class name, while the table attribute is used to specify the database table name.
2. meta: It can be used to generate the class description and is an optional part.
3. Id: It specifies the table's primary key or unique key attribute. The column attribute of the id element relates to the column in the database table, while the name attribute of the id element corresponds to the property in the class.
4. generator: It belongs to the id element as a sub-element. It is employed to produce the id automatically. Since the generator element's class property is set to native, hibernate can build the primary key using either the identity, sequence, or Hilo algorithms.
5. property: It is employed to specify how a POJO class property is mapped to a database table column. The element's name attribute corresponds to the property in the class, and the column attribute refers to the column in the database table. The type attribute contains the hibernate mapping type, which will translate from a Java data type to a SQL data type.
6. hibernate-mapping: It is the primary component in hibernate mapping files.
Syntax of Hibernate Mapping
The syntax of Hibernate Mapping in Hibernate Mapping Files is given below:
<hibernate-mapping>
<class name=" Name of the POJO class" table="Name of the table in database">
<id name="propertyName" column="columnName" type="propertyType" >
<generator class="generatorClass"/>
</id>
<property name="propertyName1" column="colName1" type="propertyType " />
<property name="propertyName2" column="colName2" type="propertyType " />
……
<property name="propertyNameN" column="colNameN" type="propertyType " />
</class>
</hibernate-mapping>
Example of Hibernate-Mapping
Let's have a look at a POJO class whose hibernate mapping files is described in the following section.
public class Student
{
private String Name;
private int roll;
public Employee() {}
public Employee(String Name, int roll ) {
this.Name = Name;
this.roll = roll;
}
public String getName() {
return Name;
}
public void setName( String name ) {
this.Name = name;
}
public int getRoll() {
return salary;
}
public void setRoll( int roll ) {
this.roll = roll;
}
}
Hibernate Mapping files for above POJO class is :
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "Student" table = "STUDENT">
<meta attribute = "class-description">
// This class contains the detail of the student.
</meta>
<id name = "id" type = "int" column = "id">
<generator class="native"/>
</id>
<property name = "Name" column = "Name" type = "string"/>
<property name = "Roll" column = "rollnumber" type = "int"/>
</class>
</hibernate-mapping>
The above hibernate mapping files defined instructs Hibernate on how to map the specified class or classes to the database tables.
Format for saving the mapping document in the file is <className>.hbm.xml.
Frequently Asked Questions
What is HQL?
Hibernate query language is known as HQL. Complex SQL queries are made simpler by this query language since it is so easy to use, effective, and object-oriented. You write queries using objects rather than tables.
What is ORM?
Object Relational Mapping is referred to as ORM. It is a programming paradigm that is used to persist Java objects to database tables. It converts data between type systems that cannot coexist within relational databases and OOP languages.
Explain lazy loading in hibernate?
It is a fetching technique for all the entities. While loading the parent class object, it chooses whether to load a child class object. The fetching method must be defined when using association mapping in Hibernate. It is mostly used to retrieve required objects from the database.
Define persistent class.
Java classes that require their objects to be saved in database tables are known as persistent classes. They must adhere to a few straightforward Java Object Model conventions (POJO).
Conclusion
Hibernate mapping in Hibernate Mapping Files can implement mappings using XML files. These mappings act as the structure for a database built to satisfy the requirements of the business model. It contains the information mapping data between a database table name and a POJO class name. It also maps data from columns in a database table to information in a POJO class.
In this blog, we discuss Hibernate Mapping files with detailed explanations. If you want to explore more blogs on this topic, please follow these blogs especially curated for readers like you - Java Framework, JDBC Connection, Guide to ORM, Introduction to Spring Boot.
Please refer to our guided pathways on Code studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.
Please do upvote our blogs if you find them helpful and informative!
Happy learning!