04 Apr 2018

Generating custom type ID in Hibernate

java, MySQL 0 Comment

If we want to customize our ids or any other columns of the tables, we can do that in Hibernate without adding the extra tables or columns to the table.

Here we are creating the simple example by using the org.hibernate.usertype.UserType, to generate the customized id.

First we are creating the simple table in MYSQL database:

Following is the Coupon bean:


We will use following CustomId value object to generate our own type of Id


So we want to store the data into the COUPON table with the customized id type, for that we need to implement the interface org.hibernate.usertype.UserType. And need to specify which one is the value class to generate the custom id by using one of this interface method returnedClass().

Following is the implementation for CustomIdType class by providing the implementation for the UserType interface.


The brief description about above methods as follows:

sqlTypes: This method returns an array of int, telling Hibernate which SQL column types to use for persisting the entity properties.

returnedClass: This method specifies which Java value type is mapped by this custom type.

assemble: Hibernate calls this method when the instance is fetched from the second-level cache and converted back from binary serialized to the object form.

disassemble: Hibernate may cache any value-type instance in its second-level cache. For this purpose, Hibernate calls this method to convert the value-type instance to the serialized binary form. Return the current instance if the value type implements the java.io.Serializable interface; otherwise, convert it to a Serializable object.

deepCopy: This method creates a copy of the value type if the value type is mutable; otherwise, it returns the current instance. Note that when you create a copy of an object, you should also copy the object associations and collections.

equals: This method compares two instances of the value type mapped by this custom type to check whether they are equal.

hashCode: This method returns a hashcode for the instance, consistent with persistence equality.

isMutable: This method specifies whether the value type is mutable. Since immutable objects cannot be updated or deleted by the application, defining the value type as immutable allows Hibernate to do some minor performance optimization.

nullSafeGet: This method constructs the value-type instance when the instance is retrieved from the database. resultset is the JDBC ResultSet object containing the instance values, names is an array of the column names queried, and owner is the persistent object associated with the value-type instance. Note that you should handle the possibility of null values.

nullSafeSet: This method is called when the value-type instance is written to a prepared statement to be stored or updated in the database. Handle the possibility of null values. A multi-column type should be written to parameters starting from index.

replace: Assume that the application maintains an instance of the value type that its associated session has already closed. Such objects are not tracked and managed by Hibernate, so they are called detached. Hibernate lets you merge the detached object with a session-managed persistent object through the session’s merge() method. Hibernate calls the replace() method when two instances, detached and session-managed, are merged. The first and second arguments of this method are value-type instances associated with a detached and session-managed persistent object, respectively. The third argument represents the owner object, the persistent object that owns the original value type: Coupon in our case. This method replaces the existing (target) value in the persistent object we are merging with a new (original) value from the detached persistent object we are merging. For immutable objects or null values, return the first argument. For mutable objects, return at least a copy of the first argument through the deepCopy() method.

We need to configure the Hibernate configuration file. The example of configuration file as follows,


We need to write the mapping configuration for the Coupon to COUPON table, so as follows,


And also we need to write the mapping for CustomIdType to tell the hibernate we are using the CustomId as one of the type for our application like as follows,



OK, all set. This is the time to test our application, following is the test client program using the Java main method.



Hey, don’t forget to have hibernate cfg file available to classpath.

All The Best 🙂