Friday, April 5, 2013

Hibernate - How to query an entity with selected columns only

In Hibernate usually when we query an entity from database it contains all the data of that row, it means all the data in the columns are contained in that entity object.
For an example assume that you have a table named as user which contains columns id, name, age and address. When you want to query a user row usually you use a query like this.

select user from User user where user.id = 10

You know the returned User object contains all the data of the row(ie. id, name, age and address)
But what can you do if you need to query id, name and age only.
Yes, one solution is to use the required columns in to the query by separating with commas as below and getting an array of values as the result.
select user.id, user.name, user.age from User user where user.id = 10

But there is another way to do this in Hibernate.
You can use a query like this.
select new User(user.id, user.name, user.age) from User user where user.id = 10

Now you get a User object which contains values of id, name and age only. Yes it is that much easy :)

Important: In order to query like this you need to have the appropriate constructor in your entity. For an example according to this example your User class should have following constructor.
public User(Integer id, String name, Integer age) {
  this.id = id;
  this.name = name;
  this.age = age;
}

Java - Case insensitive Map

You know keys in the java.util.HashMap are case sensitive. It means you can keep both "abc" and "ABC" as keys in the same map.

See this example.
public static void main(String[] args) {

  Map<String, String> map = new HashMap<String, String>();
  map.put("abc", "abc");
  map.put("xyz", "xyz");
  map.put("ABC", "ABC");

  System.out.println(map);
}
Output
{ABC=ABC, abc=abc, xyz=xyz}

But some times you may need to have a case insensitive map.
In such case you can use the java java.util.TreeMap, which let you to pass a Comparator to it's constructor.

See below example
public static void main(String[] args) {

  Map<String, String> map = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
  map.put("abc", "abc");
  map.put("xyz", "xyz");
  map.put("ABC", "ABC");

  System.out.println(map);
}
Output
{abc=ABC, xyz=xyz}

Note:
Another option is to write your own class by extending the HashMap class and overriding the get() and put() methods.

java - How to call/execute a method and continue without waiting for it

Sometimes you may have found some situations where you need to call another method from one of your methods, but need to continue the current method without waiting for the completion of the other method.
The way to accomplish this task is this.

First create a Runnable object. Inside the run() method call the second method like below.
private Runnable r = new Runnable() {
  @Override
  public void run() {
    sayHello();//Your method
  }
};
Now when you want to call that method call it like this.
new Thread(r).start();

Following is a working example
public class MyClass {

  private static Runnable r = new Runnable() {
    @Override
    public void run() {
      sayHello();
    }
  };

  public static void main(String[] args) {
    System.out.println("before calling sayHello()");
    new Thread(r).start();
    System.out.println("I didn't wait untill sayHello() finished his job");
  }

  private static void sayHello() {
    for(int i = 0; i < 10; i++) {
      System.out.println("Hello");
    }
  }
}