Thursday, December 15, 2011

Eclipse Memory Enhancements

Default Eclipse settings are optimal for machines running on 1 GB/low memory. So if you have a machine with high memory, you can use below settings to optimize performance. Add the following settings in eclipse.ini(residing in your eclipse home directory) file and start/restart the eclipse.

-Xmn128m
-Xms1024m
-Xmx1024m
-Xss2m
-XX:PermSize=128m
-XX:MaxPermSize=128m
-XX:+UseParallelGC

Thanks.

Wednesday, October 05, 2011

SQL Restriction In Hibernate Criteria Query

Here is an example to add a SQL Restriction in hibernate Criteria query.
Let's consider we have
  • A Student.java entity
  • A stored procedure in place which returns total marks obtained by a student in each subject.
public Class Student {
    private Integer id;
    private String name;
    .
    .
    //getters & setters methods
}
Assuming get_marks() stored procedure will result records in following format.

id Maths Enlish Science
1 78 75 23
2 74 70 97
I want to list down the Students who has got more than 90 in Maths. Here goes the Hibernate Criteria query.
Criteria c = session.createCriteria(Student.class)
		.addSqlrestriction(" {alias}.id in (SELECT id FROM get_marks() WHERE maths_total > ? ) ", new Double(90.0), Hibernate.Double);

In above statement {alias}.id will refer to the id column of Student table. The above addSqlrestriction example shows the SQL restriction statement with only one parameter.
What if you have to pass multiple arguments?
In such cases you need to pass array of Objects & array of Type as second & third parameter to the addSqlrestriction(String, Object[], Type[]). And in SQL String have ? where ever argument is required, as shown in above example.

I hope this helps.
Thanks.

Sunday, August 28, 2011

GTalk - Show Current Music Track - Bug: Workaround Fix

Hi All,

I have Windows XP, Google Talk 1.0.0.104 and Winamp 5.6.

GTalk is crashing whenever I'm setting my status message to "Show Current Music Track" (Winamp Playing). Here is a simple fix for this issue.

1. Open notepad
2. Click on File --> Save
3. Save file it under Winamp installation directory [ "C:\Program Files\Winamp" - in my case] with file name as "winamp.m3u".

Restart Winamp & GTalk.

This is it !!! :)

Tuesday, August 23, 2011

Khatarkun, Khatarkun, Khatarkun

Last Friday, I got bored of regular work. I was working with a colleague(and a good friend) at work and after a while our discussion turned into gossip. And my wickedly awesome head created what you are about to read...he he he !!! :D

khatarkun, khatarkun, khatarkun
mere dil ki aawaz sun...

tattos hai latest addiction uske,
naam uska lete he, sabke kaan hote hai suuunnnn,
khatarkun, khatarkun, khatarkun
mere dil ki aawaz sun...

log kehte hain, log kehte hain pj ko pyaar hua hai,
kabhi wo bhi to bole, meri ishq ki kisse sun,
khatarkun, khatarkun, khatarkun
mere dil ki aawaz sun...

hairband chode hai usne,
system me ghus coding karti hai wo,
kalyan kar uska bhi,
mere khuda, dua hum sab ki sun
khatarkun, khatarkun, khatarkun
mere dil ki aawaz sun...

naye hai additions, nayi hai value,
internal to internal,
external sources bhi hua hai guum,
khatarkun, khatarkun, khatarkun,
mere dil ki aawaz sun...

bade dino baad aaya koi chera nazar me
aaj pata chala hai naam uska, aaj hai dil me naya junoon
khatarkun, khatarkun, khatarkun
mere "bhi" dil ki aawaz sun...



This is just for fun, no offense !!!! :)

Wednesday, August 10, 2011

Hibernate Interceptors

Are you using hibernate and thinking of using Database Triggers? Then wait....

If most of your database operations are done via stored procedures then having triggers in place is the only choice to intercept any DML calls.

But since we are using hibernate we need a more object oriented way of handling things.
Hibernate interceptors is similar to triggers in database. Monitoring DML operations via triggers is the most efficient way, but it won't be easy to maintain, understand and manipulate business logic when all database operations are done via hibernate.

Coming straight to the point, hibernate provides "org.hibernate.Interceptor" interface to have the business logic to perform pre or post operations around DML hibernate calls. Following methods are required to be implemented against "org.hibernate.Interceptor".

public interface Interceptor {

public void afterTransactionBegin(Transaction arg0) {

}

public void afterTransactionCompletion(Transaction arg0) {

}

public void beforeTransactionCompletion(Transaction arg0) {

}

public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2,
Object[] arg3, String[] arg4, Type[] arg5) {
return null;
}

public Object getEntity(String arg0, Serializable arg1)
throws CallbackException {
return null;
}

public String getEntityName(Object arg0) throws CallbackException {
return null;
}

public Object instantiate(String arg0, EntityMode arg1, Serializable arg2)
throws CallbackException {
return null;
}

public Boolean isTransient(Object arg0) {
return null;
}

public void onDelete(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
}

public boolean onFlushDirty(Object arg0, Serializable arg1, Object[] arg2,
Object[] arg3, String[] arg4, Type[] arg5) throws CallbackException {
return false;
}

public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
return false;
}

public boolean onSave(Object arg0, Serializable arg1, Object[] arg2,
String[] arg3, Type[] arg4) throws CallbackException {
return false;
}

public void postFlush(Iterator arg0) throws CallbackException {

}

public void preFlush(Iterator arg0) throws CallbackException {

}
}
Since you won't be interested in implementing all of the methods, hibernate provides "org.hibernate.EmptyInterceptor" class which has empty implementation of the Interceptor interface. Now, as per your need you can override any of the desired method. Below is the sample class which I plugged in hibernate configuration to print out the primary key of any object which is getting saved.

package com.test.interceptor;

import java.io.Serializable;

import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;

public class InsertMonitorInterceptor extends EmptyInterceptor{

@Override
public boolean onSave(Object entity, Serializable id, Object[] state,
String[] propertyNames, Type[] types) {
System.out.println("Saving..." + id + ": " entity);
return super.onSave(entity, id, state, propertyNames, types);
}
}
Once you have your Interceptor class implementation ready it can be very easily plugged with any existing hibernate configuration as shown below.


new Configuration().configure(DBUtil.class.getResource("hibernate.cfg.xml"))
.setInterceptor(new InsertMonitorInterceptor())
.buildSessionFactory();
That's it. You can have different interceptor implementation plug it in to start using them.
Hope this article will be helpful. happy Intercepting.

Thanks.