• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Mapping a UUID in Hibernate  RSS feed

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to have a class that contains a UUID id attribute e.g.


My database table for X contains a VARCHAR(36) because a UUID is 32 characters long plus the 4 dashes.
How can I therefore tell Hibernate to make the custom mapping to UUID?
In a previous attempt, I had my class X contain a string id so that the setters and getters expected / returned String. In addition I had custom setters / getters that expected / returned UUID for stronger typing to the benefit of the rest of my application logic...

Thanks,
David.
 
David Brossard
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW I've been reading this: http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html

It's an excellent reference. Just posting it here for future use / reference
 
David Brossard
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A workaround although I'd appreciate a cleaner option:

- specify your POJO attribute as String
- write getters and setters w/ the appropriate type e.g. UUID. The setters and getters have to convert between what they return / take in and the actual attribute (String)
- in the hibernate mapping file for that POJO specify that Hibernate should access the field directly (access="field") rather than use the getters / setters.

If anyone's got a better way, please write it here.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!