Hi! At this moment, i will introduce basic understanding of ObjectRelational Mapping and Object-Oriented Based Query. Firstly i will discuss about ObjectRelational Mapping (ORM). ORM is a method that map all tables in a database into classes. So, if we have 3 tables, than we will have 3 classes. The benefits of ORM method is make your application ObjectOriented. The ORM has it’s own query. Somewhat called Objec-Oriented Based Query. Nowadays, There are 2 big main ObjectRelational Mapper Framework :
1. Hibernate Framework
2. JavaPersistence API
The Benefit of this query is , we don’t have to think native SQL that provided by the DBMS. For example, if you have a project that using MySQL and someday, your customer want to migrate it’s database to oracle, you don’t have to change ALL the query because the characteristics of this query is global. If you use native query, the changes are take so minimal. you don’t have to change the whole entire query.
Now, how does ORM works? Check this figure below :
Suppose we have 2 tables, employees and departments, Departments – Employees is OneToMany relationship. The ORM Framework will see these 2 tables and then convert these 2 tables then it would like something like this :
Note that the 2 tables are converted into 2 classes. Two things you have to understand :
1. In Employee class, there is Department class. It means that Employee object HAS-A Department Object. For example, Hans Kristanto is Employee, and IT Department is Department. In old way, we will store the data in database with something like this :
-emp_id = 1
-emp_name = Hans Kristanto
-dept_id = 12 (Suppose 12 is ID of IT Department)
But with ORM way, The employee object store the whole of Department Object. So, the benefit will appear in query. We will discuss it further in HQL.
2. In Department class, there is a Collections with Generic of Employee. It means that the Department class store all Object that is related with Employee. For example, if we have 3 employees in dept-1 and 2 employees in dept-2, than the 3 employees would be stored in a collections which department name is dept-1 and same with the other one.
Note that all classes are filled with data during execution.
Hibernate Query Language
Hibernate Query Language is one of Object-Oriented Based Query. There are so many Object-Oriented Based Query such as JPQL and EJBQL. Not too different between these language.
Now, how to do a query in ORM? I will refresh it first. In the past, if we want to call all employee, we use this query :
SELECT * FROM employees
Now with HQL, we can use something like that, but more simpler and logically easier :
select e from employees e
The result will be Object of employees! You can print the name or the id or whatever by using native object oriented :
System.out.println("ID : "+e.getEmpId())
System.out.println("Name : "+e.getName())
Now the question, how to get the department name? When i use e.getDepartment(), the value printed is the HashCode object?
Don’t get panic. Since department in employee is an Object of Department class, you can call it’s name by using this :
System.out.println("Dept. Name : "+e.getDepartment().getDeptName())
Compare this way with native SQL :
SELECT e.emp_id, e.name, d.name FROM employees e, departments d WHERE e.dept_id = d.dept_id
whooo… a bit complicated… we don’t have anytime to do such a complicated things😀
Now, further query.. How about if we want to populate all employees whose department is 31 (Suppose dept 31 is Financial Department). There are 2 ways in creating query for this situation :
select e from employees e where e.department.deptId = '31'
select e from employees e join e.department d where d.deptId = '31'
What is the different between theses query? NO, no significant different. Only in the second query, we give an alias in the join, while the first query not..
Okay, that’s all for the introduction. I’m preparing the tutorial in using Hibernate with Netbeans. So, stay tune here!