Using Composite Id in Hibernate 4.2.1.Final

Using composite Id with auto-generated values

If you are planning to use an entity with composite Id in Hibernate as shown in Listing 1, then you cannot auto-generate values of @Id using @GeneratedValue(strategy=GenerationType.IDENTITY).

If you attempt to do so then you’ll get the following exception:
java.lang.IllegalArgumentException: Can not set java.lang.Long field somepackage.StatementId.sequence to$2

This is a bug in Hibernate HHH-064 which also exists in version 4.2.1.Final

The work around is to explicitly generate @Id value within your Java code instead of using auto-generated value.

public class Statement {

Configuring Unicode support in Spring + Hibernate + MySQL + JSP Application

In this blog I will show you how I have configured Unicode in Spring Hibernate and MySQL project where we are using JSP for the client side.

First the data source bean configuration needs to be modified as follows:

<bean id=”dataSource” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>
<property name=”driverClassName”>
<property name=”url”>
<value>jdbc:mysql://localhost:3306/db_name?autoReconnect=true&useUnicode=true amp;characterEncoding=UTF-8</value>
<property name=”username”>
<property name=”password”>

Hibernate Performance Optimization (Part – 2)

Hibernate collection can be eager or lazy fetched which decides whether to initialize association immediately or to defer its initialization till the collection is accessed. Hibernate provides different options (fetching strategies) which decides the queries which hibernate generates to load associated collection.

Hibernate by default uses “select” fetching strategy. That means when you iterate through the collection one by one in persistent context then as soon as you access non-identifier field; hibernate will execute select query to load that item in collection. So in this way if there are 100 items in collection then for […]

Hibernate Performance Optimization (Part – 1)

To work effectively with hibernate you need to understand how hibernate executes queries. You can get the work done by using various alternative APIs which hibernate provides, but selection of correct API is needed for performance optimization. In this post I will try to explain about how hibernate uses proxy design pattern to lazy load entities and how to effectively use it for performance optimization.

About Proxy Pattern:

Proxy pattern is used when you do not want to expose the actual object, the reason can be

You want to hide the complexities of actual object. For example […]

Monitoring database connection pooling

Java Management Extensions (JMX):

Monitoring Java enabled resources either in distributed or non-distributed environments would have been thorny without JMX. What JMX technology does is that, it exposes these Java resources to be monitored using MBeans or Managed Beans. MBeans are JavaBeans really which represent the Java resources to be monitored and which can be accessed in the J2EE server. Exposing these resources as MBeans allows instrumentation. Consider a typical scenario, where Java developers troubleshoot database connection leaks, which is hard to troubleshoot. But imagine how much relaxing a Java developer would feel if  the developer […]