Thursday, January 14, 2016

Why JPA Entity or Hibernate Persistence Class Should Not be Final?

One of the interesting hibernate interview questions is, why you should not make a Hibernate persistent class final? I'll try to answer this question in this short blog post. The use of proxies is the core feature of Hibernate (one of the most popular ORM frameworks for Java Projects) for implementing key performance features e.g. lazy loading and lazy associations fetching. In order to use a proxy in place of real class, your hibernate persistence class must be either non-final or the implementation of an interface that declares all of the public methods. Why? because you cannot extend a final class in Java, and to stand up as a proxy, the proxy class must satisfy the IS-A relation, which comes either by extending a class using "extends", or implementing an interface using "implements".

Tuesday, January 5, 2016

How to check if String is not null and empty in Java?

In Java, since null and empty are two different concepts, it's a little bit tricky for beginners to check if a String is both not null and not empty. A String reference variable points to null if it has not been initialized and an empty String is a String without any character or a string of zero length. Remember, a String with just whitespace may not be considered as empty String by one program but considered as empty String by others, so, depending upon your situation, you can include the logic to check for that as well. A String with just white space is also referred to as a blank String in java. In this tutorial, I will teach you a couple of right ways to test if a String is not null and not empty in Java.

Friday, January 1, 2016 Failed to read from SocketChannel: Connection reset by peer

You might have seen the Failed to read from SocketChannel: Connection reset by peer error while working with Java NIO based server which is using SocketChannel instead of InputStream for reading data from the network. In general, this can come at both client and server end of a client-server Java application which is using TCP/IP to connect each other. Though, this exception usually comes at the server end and client was complaining that they are not able to connect. From the error message, it's quite clear that before client or server could read the data from SocketChannel, another party has disconnected the session. Let's see the root cause of the problem and how to solve Failed to read from SocketChannel: Connection reset by a peer in Java application.