ThreadLocal Usage

ThreadLocal can be used if you want variable/object to be maintained till the end of thread execution.


public class ThreadLocalTest extends Thread {
    // Define/create thread local variable
    static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();
    int serialNo;
    public ThreadLocalTest(int serialNo) {
        this.serialNo = serialNo;
    public void run() {
        threadLocal.set( serialNo );
        System.out.println( “Thread name:”+ Thread.currentThread().getName() +“\tSerialNo:” + threadLocal.get() );
    public static void main( String args[] ) {
        for ( int cnt = 0; cnt < 5; cnt++ ) {
            Thread t = new Thread( new ThreadLocalTest( cnt ), “Thread-“ + cnt );



Thread name:Thread-1 SerialNo:1

Thread name:Thread-3 SerialNo:3

Thread name:Thread-0 SerialNo:0

Thread name:Thread-2 SerialNo:2

Thread name:Thread-4 SerialNo:4


Duplicate child inserts during merge operation in hibernate

Parent entity has list of child object marked as “LAZY” with CascadeType.ALL. When I tried to add child into parent and merge it, it tries to inserts two child records.

This issue can be fixed in following ways

Solution 1:

    Retrieve the child list size() to initialize lazy child collections, add the new child and merge it.

It will insert one child record only. Ensure that it is bounded in a transaction.





Solution 2:

    Simply add the new child into parent and don’t call merge operation. Ensure that it is bounded in a transaction.



Pick up any solution that works out for you.

This is a bug in Hibernate marked as closed but not fixed

How to instruct hibernate to set DB datetime for an entity attribute(s) automatically?

Use GenerationTime to instruct hibernate to set DB date time for an attribute.


 @Column( nullable = false) 
    @Temporal( value = TemporalType.TIMESTAMP ) 
    private Date crtdttm; 

 @Column( nullable = false ) 
    @Temporal( value = TemporalType.TIMESTAMP ) 
    private Date upddttm; 

GenerationTime.INSERT – Set DB timestamp while Inserting only.

GenerationTime.ALWAYS – Set DB timestamp ALWAYS for all DML operations.

Null entities are not supported by org.hibernate.event.def.EventCache

Please ensure that you set all not nullable attributes of an entity before you perform merge operation.


@Column(name="name", length = 30, nullable = false)
private String name;

Ensure that name attribute is being set before calling entityManager.merge(<OBJECT>)

Xml file not available in classpath/build path in Eclipse?

You can instruct eclipse to put xml file into the classpath. To do that, follow the below steps

  • Open the project properties dialog
  • Go to Java Compiler -> Building
  • If not already enabled, check “Enable project specific settings”
  • Look at the “filtered resources” field, ensure that *.xml is not in the list
  • Apply -> Ok

Content Asssist is not working in Eclipse/RAD

Naviage to Windows –>Preferences –>Java  –>Editor –> Content Assist –> Advanced.

click “Restore Defaults” button

Multiple Outlook in Microsoft Windows

Use following commands to open multiple outlook in the same machine.

ExtraOutlook "C:\Program Files\Microsoft Office\Office12\OUTLOOK.EXE" /profile <PROFILE_NAME1>

ExtraOutlook "C:\Program Files\Microsoft Office\Office12\OUTLOOK.EXE" /profile <PROFILE_NAME2>

Point to correct OUTLOOK.EXE path if the above path is incorrect.