5 Differences Between Hibernate and Data Access Agility

Data Access Agility is a good alternative to traditional ORMs, such as Hibernate. In this article, we highlight the major advantages of Data Access Agility over traditional ORMs. Once you are done reading this article, you can try Data Access Agility for free by visiting Data Access Agility website!

Mapping

ORM tools bind persistence objects to underlying database tables. Hibernate mainly use xml files to define the object relational mapping; where programmers are responsible for making sure that column names and data types are specified accurately in the xml mapping file. Data Access Agility, however, provides programmers with Database Schema panel that allows them to define the database schema using an easy to use GUI. The output of this process is a DDL script that builds the database schema. The programmer also gets a set of persistence entities (E.g. Java classes) that can be included in the software project. The generated classes will already be mapped to underlying database tables.

Hibernate Queries

Hibernate provides programmers with ready-to-use API to execute basic data operations (E.g CRUD). To execute a custom operation, programmers need to write queries, either using SQL or Hibernate Query Language (HQL). For example, if a programmer needs to select the value of a single field in a database table, he will have to write a custom query that returns the target field. Data Access Agility, on the other hand, allows programmers to define highly customized data access operations using easy to use GUI. Data Access Agility will then generate custom java methods that encapsulate the data access logic that manipulates the data in the database. To update a single field, however, we have two problematic options. First, we could fetch the whole entity, change the value of the field, then save the entity back. Second solution avoids fetching the whole entity; but it requires that we write our own code.

The following code snippet shows the java code to update the value of a single column using Hibernate.

String hql = "UPDATE Employee set salary = :salary WHERE id = :employee_id";

Query query = session.createQuery(hql);

query.setParameter("salary", 1000);
query.setParameter("employee_id", 10);

int result = query.executeUpdate();

System.out.println("Number of rows affected: " + result);

The following code snippet, however, shows the code to execute the same operation by a data access method generated by Data Access Agility.

int countRowsAffected = EmployeeData.updateEmployeeSalaryById(1000, 10);

System.out.println("Number of rows affected: " + countRowsAffected);

Database Schema Version Control

Programmers always need to make changes to database schema. Hibernate does not provide any mechanism to apply the concept of version control to database schema. Data Access Agility, as mentioned before, will generate the DDL scripts without any additional effort. That is not all; Data Access Agility has the capability to generate the DDL scripts since last project revision. For example, if you need to add a new column to your database schema, Data Access Agility will generate an ALTER TABLE statement that adds the new column to the database instead of generating a CREATE TABLE statement that creates the table from scratch. Programmers can also view the changes made to database schema overtime. Moreover, they can generate the DDL scripts for the difference between two database schema revisions.

Documentation

One major challenge associated with software maintenance is the ability to understand the database structure. Table and column names are not sufficient to understand database schema; especially when abbreviations and inappropriate names are used. The situation becomes worse when a new software engineer is assigned to a established software project. Data Access Agility provides a state-of-the-art solution to mitigate such issues. Programmers can provide a short documentation for each table and column created in the Database Schema panel. The documentation will be reflected automatically on all persistence entities and data access objects as javadoc comments. So programmers can comprehend database structure while writing the business logic code.

Java Comments are not supported by Hibernate

Persistence Entities

Entities represent the domain models of our applications. Hibernate requires that programmers write the code of persistent entities. With Data Access Agility, once the database schema has been defined, the code of persistence entities will be auto generated along with javadoc comments.

/**
* Any person working for the company including part-time employees and interns
*/
public class Employee
{

	private int id;
	private String number;
	private String fullName;
	private int status;
	private java.math.BigDecimal salary;
	private int divisionId;
	private Division division;

	/**
	* Sets the value of id
	* @param val A unique number used internally by the system. This number is not supposed to be known by employees
	*/
	public void setId(int val)
	{
		this.id = val;
	}

	/**
	* Returns the value of id
	* @return A unique number used internally by the system. This number is not supposed to be known employees
	*/
	public int getId()
	{
		return this.id;
	}

	/**
	* Sets the value of number
	* @param val Employee's personnel number that is used by HR department
	*/
	public void setNumber(String val)
	{
		this.number = val;
	}

	/**
	* Returns the value of number
	* @return Employee's personnel number that is used by HR department
	*/
	public String getNumber()
	{
		return this.number;
	}

	/**
	* Sets the value of fullName
	* @param val Employee's full name as appears on his Driving Licence or Passport. No prefix (E.g. Mr) should be stored in this field
	*/
	public void setFullName(String val)
	{
		this.fullName = val;
	}

	/**
	* Returns the value of fullName
	* @return Employee's full name as appears on his Driving Licence or Passport. No prefix (E.g. Mr) should be stored in this field
	*/
	public String getFullName()
	{
		return this.fullName;
	}

	/**
	* Sets the value of status
	* @param val Employee's status could be (1) Active, (2) Suspended, or (3) On Vacation. This field should be used to control access to company's internal systems
	*/
	public void setStatus(int val)
	{
		this.status = val;
	}

	/**
	* Returns the value of status
	* @return Employee's status could be (1) Active, (2) Suspended, or (3) On Vacation. This field should be used to control access to company's internal systems
	*/
	public int getStatus()
	{
		return this.status;
	}

	/**
	* Sets the value of salary
	* @param val Employee's base salary NOT including any bonuses or allowances. Net salary, including bonuses and allowances, is calculated based on some rules coded in the business logic layer
	*/
	public void setSalary(java.math.BigDecimal val)
	{
		this.salary = val;
	}

	/**
	* Returns the value of salary
	* @return Employee's base salary NOT including any bonuses or allowances. Net salary, including bonuses and allowances, is calculated based on some rules coded in the business logic layer
	*/
	public java.math.BigDecimal getSalary()
	{
		return this.salary;
	}

	/**
	* Sets the value of divisionId
	* @param val No documentation is available
	*/
	public void setDivisionId(int val)
	{
		this.divisionId = val;
	}

	/**
	* Returns the value of divisionId
	* @return No documentation is available
	*/
	public int getDivisionId()
	{
		return this.divisionId;
	}

	/**
	* Sets the value of division
	*/
	public void setDivision(Division val)
	{
		this.division = val;
	}

	/**
	* Returns the value of division
	*/
	public Division getDivision()
	{
		return this.division;
	}
}

There exists a Hibernate Reverse Engineering tool that generates domain model classes from existing database. This tool has a built-in strategy for mapping column data types and names to property definitions in Java. The default strategy makes some assumptions about the names of objects in the database. The default mapping strategy, for example, assumes that column name parts are separated by underscore, so the column ‘customer_number’ is mapped to property ‘customerNumber’. In order to customize the strategy, the software engineer is expected to write a custom strategy class that does the magic. Data Access Agility provides the capability to explicitly specify the column (Or table) name in the database, and the property (Or class) name in Java. This solution is proposed to maximize the flexibility of code generation; especially in database engines that has strict naming policies.

Try Data Access Agility for FREE

Data Access Agility provides promotional subscriptions for free. You can visit their website at https://www.dataaccessagility.com