Hi everyone,sorry for the long topic, I started working with hibernate for a couple of weeks, so if I missed an obvious solution written in the documentation I apologize. I tried to use the interceptor class of hibernate for a specific task,after the "classic" command :
Code:
List<T> list = criteria.list();
i want to read
before the invocation of the
Code:
getEntity()
method from the interceptor class of hibernate,the values and columns of the result (you can see that like the result set from a classic sql query).
So i tried to use the method
Code:
onPrepareStatement();
(always from interceptor class) and i can catch the prepared statement builded from hibernate, so i can change the column ^^ and this is done.
No i'm try to catch the value for each column before the invocation of the
Code:
getEntity()
. A example for why i want this?
Given for a example the java object: Obj(Integer doc_id,URL url,String city);
on the database i have on the "url" column some value with protocol "http://" e some value without. So when i invoke the getEntity method on the interceptor the process launch the "MalformedUrlException" for the values without protocoll. So i just want to check if the url value has the protocol and if don't have make a append on the value.tostring(). I put a piece of code for make myself more clear on what i want to do:
NOTE 1:I know the following is a prepared statement and i can't capture the actual value of the columns, But I have shown this example just to give an idea of what I need to do.
Code:
public String onPrepareStatement(String sql) {
System.out.println("onPrepareStatement: Called for statement: " + sql);
//The SQL in input is like this:
//onPrepareStatement: Called for statement:
//select this_.doc_id as doc_id1_1_0_, this_.city as city2_1_0_
//What i want is read->modify->return the SQL to Hibernate:
//[READ]select "doc_id" as doc_id1_1_0_, "city" as city2_1_0_
//read the actual value associated a the columns...
//[MODIFY]select "doc_id2" as doc_id1_1_0_, "city2" as city2_1_0_
//...change these values...
//[RETURN]select this_.doc_id as doc_id1_1_0_, this_.city as city2_1_0_
//...return the ne prepared statement to the getEntity() methhod but with
//new value for the param
return sql;
}
//... invoke the getEntity method
NOTE 2:I know i can just use SQL\HQL native query or get the resutl set in other
ways but i just can't touch the project so i can only intercept the response,
changed and return to the project.
Ty in advance for any help.