
Private UUID String String = "password_hash") Public class UserDO = "uuid2", strategy = "id", updatable = false, nullable = false, columnDefinition = "VARCHAR(36)") Now, we create our domain class using Hibernate annotations to map it to our existing MySQL table. The rest of the table structure can be similar to the image. Since we know the format of the UUID when represented as a String, we know that it has a length of 36 characters, so we can define the column as VARCHAR(36).

Because we want all the information to be human readable as well, the ID should be stored as a String. We want the ID to be unique so we will be using the Java UUID in our domain object. Look up best practices of storing passwords in the database when creating the final structure. Storing an unsalted password hash is not recommended. *This is a simplified structure and only has a minimum number of columns. I will be using a simplified version of a USERS table that stores login information for each registered user to a site. The good news is that there is an easy way of mapping the Java UUID column to MySQL using Hibernate and no additional libraries are needed.įirst, let’s look at the a sample table. When using an UUID, this can pose some tricks, depending on the configuration and MySQL version.

I won’t be covering the auto-incremented method since it poses no real problems and can be mapped to an Integer in the Java domain class. The most common methods are to use an auto-incremented column or a generated UUID. When creating the database structure it is important to make sure that each row in a table has a unique ID so that it can be easily indexed, retrieved, and manipulated when needed.
#HIBERNATE UUID GENERATOR HOW TO#
Article originally posted on my personal website at How to use String UUID in Hibernate with MySQL - Petre Popescu
