RSS

Tag Archives: Java

LambdaJ as an alternative to query out from Collections (Stop Iterations!!)

I remember the first time i was taught Data Structure during my college time, there was a way to generate all data from a collection (ArrayList, LinkedList, etc). At that time, i was thinking that it was the best way to populate the data until a position where i learnt how to build a web application and optimization. It is not optimized at all to iterate all the data everytime we triggered an event (let say ‘key event’ when we type ‘A’ and all data consisting ‘A’ will appear). Microsoft .NET have LINQ, but how about Java? Don’t worry for all of you, there are a lot also tools to do things like this. I will highlight my current post today for LambdaJ. LambdaJ is a library to manipulate a collections without any iterations. You can also implement a ‘SQL’like syntax into it. To use LambdaJ, you have to download 3 dependencies library for this : 1. CGLib (download from here) 2. LambdaJ (download from here) 3. Hamcrest Matcher (download from here) NOTE:for Hamcrest Matcher lib, download the one with keyword all For example if you have a collections below :


List<String> listOfNames = new ArrayList<String>();
listOfNames.add("Hans");
listOfNames.add("Kristanto");

If you want to merge all the data inside of the list, instead of using for, you can use a method from LambdaJ like below :


package test.lambdaj;

import java.util.List;
import java.util.ArrayList;
import static ch.lambdaj.Lambda.*;

/**
 *
 * @author Hans Kristanto
 */
public class Main {
    
    public static void main(String[] args){
    
        List<String> list = new ArrayList<String>();
        list.add("Hans");
        list.add("Kristanto");
        
        String names = "";
        names = join(list,"-");
        
        System.out.println(names);
    }
}   

The result will be :

Hans-Kristanto

Of course it is not all LambdaJ can do, here come the funkiest stuff :D. Let say we have this list below given :


List<String> listOfNames = new ArrayList<String>();
listOfNames.add("Orange");
listOfNames.add("Mango");
listOfNames.add("Guava");
listOfNames.add("Banana");
listOfNames.add("Papaya");
listOfNames.add("Strawberry");

You want to populate all data containing letter of ‘o’ into new List, you can do like this below :


package test.lambdaj;

import java.util.List;
import java.util.ArrayList;
import static ch.lambdaj.Lambda.*;
import static org.hamcrest.Matchers.*; //IMPORTANT TO DO A MATCHER STUFF

/**
 *
 * @author Hans Kristanto
 */
public class Main {
    
    public static void main(String[] args){
    
        List<String> listOfNames = new ArrayList<String>();
        listOfNames.add("Orange");
        listOfNames.add("Mango");
        listOfNames.add("Guava");
        listOfNames.add("Banana");
        listOfNames.add("Papaya");
        listOfNames.add("Strawberry");
        
        List<String> listOfNewFruit;
        listOfNewFruit = select(listOfNames, having(on(String.class),containsString("o")));
        
        String names = join(listOfNewFruit,"~");

        System.out.println(names);
    }
}   

And the result will be :

Mango

Awesome is it? It will be much more useful for you if you have a case like you have bunches of data generated from DB for example, but you dont want to periodically retrieve from the DB since the data always the same. This can be much more helpful for you. You can refer more examples from javaone slides below : LambdaJ Slides.

 
3 Comments

Posted by on August 6, 2011 in English, Programming

 

Tags: , , , , , , , ,

Basic Introduction of Object Relational Mapping and HQL

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 :

Graphic1

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 :

Conversion

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'

OR

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!

 
2 Comments

Posted by on August 1, 2009 in English, Programming

 

Tags: , , ,