Changing the default type mapping
Sometimes we are interested in changing JPA's default type mapping strategy. For example
Mysql™
versions prior to 5.0 lack an appropriate type representing boolean
values. It was therefore quite common mapping boolean properties to
CHAR(1)
with possible values being 'Y'
and
'N'
. Hibernate will map boolean values to
tinyint(1)
. Supporting older software may require to
tweak the standard mapping.
Unfortunately JPA itself does not offer
any interface for this purpose. The persistence provider may offer a
solution though. Hibernate for example allows to remap ❶ types . We assume our
hibintro.v9.User
class to have a
boolean
property active
:
Java |
|
Sql |
|
Readers being interested in more sophisticated strategies like mapping user defined data types to database types are advised to read the manual section on Hibernate types.