• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mapping a UUID in Hibernate

 
David Brossard
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic