Resolve persistent classes and mapping files in Java's Hibernate framework

  • 2020-04-01 04:31:07
  • OfStack

The persistent classes
The whole concept of Hibernate is to take the value of a property from a Java class and persist it to a database table. A mapping file, Hibernate, helps you determine how to pull values from classes and map them to tables and related fields.

Its object or instance calls a Java class stored in a database table a persistent class in Hibernate. Hibernate works best if these classes follow some simple rules, also known as the plain Java object (POJO) programming model. There are the following main rules for persisting classes, but they are not required.

Persisting all Java classes requires a default constructor.

All classes should contain ids to make it easy to identify Hibernate and the database within the object. This property maps to the primary key column of the database table.

All properties will be persisted should be declared private and have been defined in javabean-style getXXX and setXXX methods.

Hibernate's key functionality, the proxy, depends on the implementation of persistent classes or interfaces to all public methods that are not final, or declared.

All classes that do not extend or implement the EJB framework require specialized classes and interfaces.

POJO names are used to emphasize that a given object is an ordinary Java object, not a special object, and certainly not an Enterprise javabeans.

A simple example of a POJO:
Based on some of the rules mentioned above, we can define a POJO class as follows:


public class Employee {
  private int id;
  private String firstName; 
  private String lastName;  
  private int salary; 

  public Employee() {}
  public Employee(String fname, String lname, int salary) {
   this.firstName = fname;
   this.lastName = lname;
   this.salary = salary;
  }
  public int getId() {
   return id;
  }
  public void setId( int id ) {
   this.id = id;
  }
  public String getFirstName() {
   return firstName;
  }
  public void setFirstName( String first_name ) {
   this.firstName = first_name;
  }
  public String getLastName() {
   return lastName;
  }
  public void setLastName( String last_name ) {
   this.lastName = last_name;
  }
  public int getSalary() {
   return salary;
  }
  public void setSalary( int salary ) {
   this.salary = salary;
  }
}


The mapping file
Object/relational mapping is usually defined in the XML document. This mapping file instructs Hibernate how to define one or more classes that map to database tables.

While many Hibernate users choose to write XML by hand, there are tools for generating mapping documents. Users for advanced Hibernate, including XDoclet, Middlegen, and AndroMDA.

Let's consider that our object will adhere to the POJO classes defined earlier in the table defined in the next section.


public class Employee {
  private int id;
  private String firstName; 
  private String lastName;  
  private int salary; 

  public Employee() {}
  public Employee(String fname, String lname, int salary) {
   this.firstName = fname;
   this.lastName = lname;
   this.salary = salary;
  }
  public int getId() {
   return id;
  }
  public void setId( int id ) {
   this.id = id;
  }
  public String getFirstName() {
   return firstName;
  }
  public void setFirstName( String first_name ) {
   this.firstName = first_name;
  }
  public String getLastName() {
   return lastName;
  }
  public void setLastName( String last_name ) {
   this.lastName = last_name;
  }
  public int getSalary() {
   return salary;
  }
  public void setSalary( int salary ) {
   this.salary = salary;
  }
}

There will be a table for each object, and you are willing to provide persistence. To consider the above, you need to store and retrieve the following RDBMS tables:


create table EMPLOYEE (
  id INT NOT NULL auto_increment,
  first_name VARCHAR(20) default NULL,
  last_name VARCHAR(20) default NULL,
  salary   INT default NULL,
  PRIMARY KEY (id)
);

Based on the above two entities, we can define it to indicate how Hibernate defines one or more classes that map to the mapping file below the database table.


<?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="Employee" table="EMPLOYEE">
   <meta attribute="class-description">
     This class contains the employee detail. 
   </meta>
   <id name="id" type="int" column="id">
     <generator class="native"/>
   </id>
   <property name="firstName" column="first_name" type="string"/>
   <property name="lastName" column="last_name" type="string"/>
   <property name="salary" column="salary" type="int"/>
  </class>
</hibernate-mapping>

Format in the saved mapping file: < Classname> . HBM. XML. We save the file employee.hbm.xml in the mapping file. Take a look at the small details about the mapping elements used in the mapping file:

Mapping documents are available with < Hibernate - mapping> To include all < Class> XML document for the root element of the element.

In < Class> The element is used to define a database table specific mapping from a Java class. The Java class name is specified using the name attribute of the class element and the database table name using the table attribute.

< Meta> The element is an optional element that can be used to create a description of the class.

< Id> The element maps the unique ID attribute in the class to the primary key of the database table. The name attribute of the id element refers to the class of the attribute and the column attribute refers to the column in the database table. The type attribute holds the Hibernate mapping type that will be converted from Java to SQL data type.

In the id element < Generator> The element is used to automatically generate primary key values. Set the class attribute of the generated element to native so that Hibernate can pick up the algorithm in either identity, sequence, or hilo to create the primary key based on the underlying database's supporting capabilities.

< Property> The element is used to map attributes of a Java class to columns in a database table. The name attribute of the element refers to the class of the attribute and the column attribute refers to the column in the database table. The type attribute holds the Hibernate mapping type that will be converted from Java to SQL data type.

Also, this will be used in the mapping file, and then try to cover as many other attributes and elements available for other hibernate-related topics as possible.


Related articles: