Archive for July, 2011

Regular expressions for valid IP version 4 and IP version 6 addresses

This uses java.util.regex.Pattern

Regular Expressions are

Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
Pattern IPV6_PATTERN= Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$");
Pattern IPV6_HEX_COMPRESSED_PATTERN	= Pattern.compile("^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$");

Here is a demo

public static void main(String args[])
 String exampleIP="";
 System.out.println("Is it a valid IP?:"+IPV4_PATTERN.matcher(exampleIP).matches()); 


Let me know if you have any questions.

Why use Hibernate Framework

Hibernate is concerned with helping your application to achieve persistence. So what is persistence? Persistence simply means that we would like our application’s data to outlive the applications process. In Java terms, we would like the state of (some of) our objects to live beyond the scope of the JVM so that the same state is available later.

Relational Databases
Specifically, Hibernate is concerned with data persistence as it applies to relational databases (RDBMS). In the world of Object-Oriented applications, there is often a discussion about using an object database (ODBMS) as opposed to a RDBMS. We are not going to explore that discussion here. Suffice it to say that RDBMS remain a very popular persistence mechanism and will so for the foreseeable future.

The Object-Relational Impedence Mismatch
‘Object-Relational Impedence Mismatch’ (sometimes called the ‘paradigm mismatch’) is just a fancy way of saying that object models and relational models do not work very well together. RDBMSs represent data in a tabular format (a spreadsheet is a good visualization for those not familiar with RDBMSs), whereas object-oriented languages, such as Java, represent it as an interconnected graph of objects. Loading and storing graphs of objects using a tabular relational database exposes us to 5 mismatch problems…

1. Granularity
Sometimes you will have an object model which has more classes than the number of corresponding tables in the database (we says the object model is more granular than the relational model). Take for example the notion of an Address…

2. Subtypes (inheritance)
Inheritance is a natural paradigm in object-oriented programming languages. However, RDBMSs do not define anything similar on the whole (yes some databases do have subtype support but it is completely non-standardized)…

3. Identity
A RDBMS defines exactly one notion of ‘sameness’: the primary key. Java, however, defines both object identity (a==b) and object equality (a.equals(b)).

4. Associations
Associations are represented as unidirectional references in Object Oriented languages whereas RDBMSs use the notion of foreign keys. If you need bidirectional relationships in Java, you must define the association twice.

Likewise, you cannot determine the multiplicity of a relationship by looking at the object domain model.

5. Data navigation
The way you access data in Java is fundamentally different than the way you do it in a relational database. In Java, you navigate from one association to an other walking the object network.

This is not an efficient way of retrieving data from a relational database. You typically want to minimize the number of SQL queries and thus load several entities via JOINs and select the targeted entities before you start walking the object network.

Benefits of using Hibernate:

Natural Programming Model: Hibernate lets you develop persistent classes following natural Object-oriented idioms including inheritance, polymorphism, association, composition, and the Java collections framework.

Transparent Persistence: Hibernate requires no interfaces or base classes for persistent classes and enables any class or data structure to be persistent. Furthermore, Hibernate enables faster build procedures since it does not introduce build-time source or byte code generation or processing.

High Performance: Hibernate supports lazy initialization, many fetching strategies, and optimistic locking with automatic versioning and time stamping. Hibernate requires no special database tables or fields and generates much of the SQL at system initialization time instead of runtime. Hibernate consistently offers superior performance over straight JDBC coding.

Reliability and Scalability: Hibernate is well known for its excellent stability and quality, proven by the acceptance and use by tens of thousands of Java developers. Hibernate was designed to work in an application server cluster and deliver a highly scalable architecture. Hibernate scales well in any environment: Use it to drive your in-house Intranet that serves hundreds of users or for mission-critical applications that serve hundreds of thousands.

Extensibility: Hibernate is highly customizable and extensible.
Comprehensive Query Facilities: Including support for Hibernate Query Language (HQL), Java Persistence Query Language (JPAQL), Criteria queries, and “native SQL” queries; all of which can be scrolled and paginated to suit your exact performance needs.

Features of Hibernate

Hibernate 3.0 provides three full-featured query facilities: Hibernate Query Language, the newly enhanced Hibernate Criteria Query API, and enhanced support for queries expressed in the native SQL dialect of the database.

Filters for working with temporal (historical), regional or permissioned data.

Enhanced Criteria query API: with full support for projection/aggregation and subselects.

Runtime performance monitoring: via JMX or local Java API, including a second-level cache browser.

Eclipse support, including a suite of Eclipse plug-ins for working with Hibernate 3.0, including mapping editor, interactive query prototyping, schema reverse engineering tool.

Hibernate is Free under LGPL: Hibernate can be used to develop/package and distribute the applications for free.

Hibernate is Scalable: Hibernate is very performant and due to its dual-layer architecture can be used in the clustered environments.

Less Development Time: Hibernate reduces the development timings as it supports inheritance, polymorphism, composition and the Java Collection framework.

Automatic Key Generation: Hibernate supports the automatic generation of primary key for your.

JDK 1.5 Enhancements: The new JDK has been released as a preview earlier this year and we expect a slow migration to the new 1.5 platform throughout 2004. While Hibernate3 still runs perfectly with JDK 1.2, Hibernate3 will make use of some new JDK features. JSR 175 annotations, for example, are a perfect fit for Hibernate metadata and we will embrace them aggressively. We will also support Java generics, which basically boils down to allowing type safe collections.

EJB3-style persistence operations: EJB3 defines the create() and merge() operations, which are slightly different to Hibernate’s saveOrUpdate() and saveOrUpdateCopy() operations. Hibernate3 will support all four operations as methods of the Session interface.

Hibernate XML binding enables data to be represented as XML and POJOs interchangeably.

The EJB3 draft specification support for POJO persistence and annotations.

Above text is taken from and

How to get Exception Stacktrace to String

If you ever wanted to execute something like:


Then you have come to the right place.


will return a string which would contain stacktrace. Later, you can log that in a DB or in a file or just print on console.

    public String getStringStackTrace(Throwable t)
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw, true);
            return sw.toString();

Here is one usage example:


public class Test {

	public static void main(String args[])

					throw new RuntimeException("Test getStringStrackTrace");
				} catch (Exception e)



java.lang.RuntimeException: Test getStringStrackTrace
	at com.trgr.cobalt.performance.integrationtests.Test.main(