Saturday, August 15, 2015

Hibernate Discriminator Column Not Getting Auto-populated For InheritanceType.JOINED

I am new to Object Relational Mapping as I have developed database applications with plain JDBC all these years. So I am using Hibernate now and I wanted to have separate extension table for the new subclass that I have created. But I also wanted to treat existing superclass as a separate entity that can be queried without getting results from subclass too. Suppose I have Customer class. And I have created EliteCustomer subclass. I have a requirement that I can query Customer class that doesn't include results from EliteCustomer class. It's a bad example but I guess you got what I needed to do. So I thought of using InheritanceType.JOINED with discriminator column. Then by using discriminator value I can query only superclass objects. But while inserting data through Hibernate, discriminator column was not getting auto-populated and it was storing null value. And if I try to make discriminator column not null, insertion started failing. Now the fact is that Hibernate doesn't really require discriminator column for joined inheritance like some other JPA providers do. It just works fine if you don't mention any discriminator column. But for my case I needed to treat superclass as separate entity while querying, that's why I needed the discriminator column. And JPA spec does support discriminator column for joined inheritance type. So I thought it would be alright. Anyways I found out that some old versions of Hibernate don't support this legacy behaviour. Here is the Jira ticket details that was opened for this issue. As I was using Hibernate 3, I was facing the problem. If you want to turn on support for discriminator column for joined inheritance type, just upgrade to correct 4.2 or 4.3 version and set hibernate.discriminator.ignore_explicit_for_joined to false as mentioned in Jira details. Then Hibernate will start auto-populating value into your discriminator column.

Tuesday, July 14, 2015

A List Of Websites To Download Youtube Video Subtitles

If you want to download subtitles from Youtube videos, there are some software available. I tried Google2SRT. But when I installed it, it installed some additional software that changed my browser homepage and search settings. It happened for all the browsers including Firefox and Google Chrome. I didn't like it and uninstalled and cleaned up all these software.
And I went for second option. Use websites where you can download Youtube video subs online without installing any software in your computer. Below I have mentioned three such websites where you can download Youtube video subtitles for free. All of them are currently working. So if any of them is down for some reason at a particular time, you can always use the other alternatives.

Saturday, July 4, 2015

Google Chrome Update Now (Quick Search) Malware Fix

Recently whenever I tried to open any normal http website in my Google Chrome browser, I kept on getting a redirection showing message that Chrome needed to be updated:
Google Chrome browser has critical security update. Auto update is disabled, you need manually perform update browser.

You need to perform the update in order to continue your web navigation.
Google Chrome Update Now (Quick Search)
But Google Chrome was already updated to the latest version. This looked liked a malware. I tried uninstalling Chrome and then again installing from scratch. I also tried running some malware/spyware removal tools. But still Chrome was having same issue. Now today I just again did a complete reinstall & also ran Google Software Removal Tool. This tool checks for any unwanted program that might have affected your Google Chrome browser and resets the settings. After that now my Chrome is working fine. If you think Google Chrome is behaving weirdly for some reason, you can download and run this software to see if it can fix it.

Sunday, June 28, 2015

Why You Do Not Need IDM To Download Online


It's not like I hate Internet Download Manager. In my college days, I used to use it a lot for download acceleration and resume support for my slow internet connection. It was quite fast and reliable. Only glitch it seemed to have was that download would still go on indefinitely few times even after 100% was done. I don't know what was the exact issue that caused it. Anyways, I am pretty sure if there was any issue they would have fixed it by now as I last used it several years back.



So that's my point. Why haven't I used such a popular software like IDM? Reason is simple. I didn't need it as I have had a equally good free alternative for my download purpose. Nowadays I would mostly use direct browser download using Chrome or Firefox browser. And when I still feel a need for some acceleration or resume support, I will go with Free Download Manager. It's a popular open source software. And it does its job quite well. So unless you don't want something specific, then for your general purpose online downloads I don't see any point in buying paid license for IDM. Try out Free Download Manager first and check its feature list. Most likely you will find it good enough.

Thursday, May 21, 2015

Tomcat Not Accessible Outside Due To Blocked Port

You have installed Tomcat in your Linux machine. You can access it inside the machine using localhost:8080 or 127.0.0.1:8080. But when you try to connect to it remotely from another machine, socket connection is timing out and you are not able to access tomcat. That can be because of that specific port is blocked in Linux firewall. There's an easy way to check. Try to create a telnet connection to the specific host and port like:
telnet host-name 8080
If the connection is denied or you get a timeout, then the port is not open. So now question is how you can open the port from Linux firewall. To open a port for TCP connection is easy in Linux machine. You just have to use iptables program. Open a command line. Type below command:
iptables -I INPUT -p tcp --dport 8080 --syn -j ACCEPT
That's all. Now you can try to access tomcat on port 8080 remotely and it should work.
You may want to execute the following command after this:
service iptables save
This will persist the changes you made in iptables across system restarts.

Java Thread Interruption With join() Method Explained

This is an example how Java thread interrupt() works with join() method. Suppose there are two threads, t1 & t2. So t2 calls join method on t1 from a method like below:
try {
     t1.join();
} catch (InterruptedException e) {
      System.out.println("INTERUPPTED!");
}
Now the question is when will code enter into catch block. Do you need to call interrupt() method on the calling thread which in this case is t2? Or do you need to to call interrupt() on the thread on which join is called which in this case is t1?

I have written a simple program that will clarify this.


public class TestInterrupt {
   
    public static void main(String[] args) {
         R2 r2 = new R2();
         Thread t2 = new Thread(r2);
         t2.start();
    }
   
    private static class R1 implements Runnable {
        public void run() {
            while(true) {
                System.out.println("do nothing!");
            }
        }
    }
    private static class R2 implements Runnable {
        public void run() {
            R1 r1 = new R1();
            Thread t1 = new Thread(r1);
            t1.start();
            t1.interrupt();
            try {
                t1.join();
            } catch (InterruptedException e) {
                System.out.println("INTERUPPTED!");
                System.exit(0);
            }
        }
    }

}

If you look at the above code, basically t2 calls t1.join() method and before that it calls t1.interrupt() method. Now if you run it, it will keep on printing "do nothing" in the console. So that means using interrupt on the thread on which join() is called is not working. Otherwise it would have exited the program.
Now run the below code:


public class TestInterrupt {
   
    public static void main(String[] args) {
         R2 r2 = new R2();
         Thread t2 = new Thread(r2);
         t2.start();
    }
   
    private static class R1 implements Runnable {
        public void run() {
            while(true) {
                System.out.println("do nothing!");
            }
        }
    }
    private static class R2 implements Runnable {
        public void run() {
            R1 r1 = new R1();
            Thread t1 = new Thread(r1);
            t1.start();
            Thread t2 = Thread.currentThread();
            t2.interrupt();
            try {
                t1.join();
            } catch (InterruptedException e) {
                System.out.println("INTERUPPTED!");
                System.exit(0);
            }
        }
    }

}

It calls t2.join(). And if you execute it, the code would exit after a few seconds after printing "INTERUPPTED!". So that's how it works. You need to interrupt the thread that is calling join() method on another thread and it will throw InterruptedException.

Wednesday, May 6, 2015

How To Find Out How Many CPU Cores You Have In Windows

From command prompt, execute below command:
WMIC CPU Get /Format:List

Then check for NumberOfCores value. That will give total number of physical CPU cores that you have in you machine. And look for NumberOfLogicalProcessors value too. If Hyper-Threading is enabled, you will get double of total number of physical cores.
You can also go to Task Manager and check CPU Usage History under Performance tab. But the number of boxes there basically represents all the logical processors in your machine. My Windows laptop is dual core with Hyper-Threading enabled. So I will get four boxes there. But that doesn't represent the total number of physical CPU cores that I have which is 2. And there is no way to differentiate logical processors and physical cores there.


So it's better to use the above command. That will clearly display total number of physical cores and logical processors on a single screen.