Home Ask Login Register

Developers Planet

Your answer is one click away!

Santosh February 2016

How to intercept hibernate query by column type

I am trying to intercept Hibernate query execution, Here is my hibernate mapping

<class name="com.model.Parent" table="Parent"
        optimistic-lock="version" dynamic-update="true" dynamic-insert="true"
        lazy="true" >
<property name="id"
            type="org.hibernate.usertype.UserType" column="OBJECT_ID" />
 ( Here UserType is GUID format).....

If the generated query like the following

select * from Parent p_ where p_.id = ? OR p_.id in (?,?,?) 

need to replace ? with HEXTORAW(?) for tables (columns defined with RAW). Required query should be like this

select * from Parent p_ where p_.id = HEXTORAW(?) OR p_.id in (HEXTORAW(?),HEXTORAW(?),HEXTORAW(?)) 

Since I am working with existing system, can't change database column types.

How to find columns defined by RAW on the query execution (if i use interceptors or any other filters)? and how to apply HEXTORAW() on those found columns?


shazin February 2016

You can use Interceptor.onPrepareStatement and Register it the SessionFactory

But you would have to make sure that the interceptor modifies only the required field parameters to HEXTORAW() not all.

Post Status

Asked in February 2016
Viewed 2,394 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life