Search code examples
javaspringhibernate-mapping

Mapping string value to integer using hibernate mapping xml


I couldn't find a solution to this so I am posting this as a question.

I basically have two databases that I am using to map values from. I am supposed to use only one entity(persistence object) but have two hbm.xml files that correspond to the two database tables. The problem I am having is that one of the key fields is stored as a VARCHAR in one table and as INT in the other. The entity field is defined as an int so the INT mapping works fine. I am not sure how to use the hibernate mapping xml to convert the VARCHAR to an int value.

The VARCHAR is not of the form of a number. It is a text i.e "car" that has a corresponding int value.

Can someone shed some light on this?


Solution

  • Ok I have figured out how to do this using the UserType class in Hibernate. You can make the implementation in the GetNullSafeSet method.