Hibernate UserType and specific length

I have a hibernate Usertype something like this:

public class UUIDHibernateType implements UserType { private static final int[] SQL_TYPES = new int[] { Types.CHAR }; public int[] sqlTypes () { return SQL_TYPES; } // ... } 

The problem is that hibernate generates a sql script with CHAR (1) types, which is wrong, I really need CHAR (36). How to determine the default length of a custom type?

At the moment, I am fixated on the definition of the sql type as follows:

 <id name="id" type="org.openscada.ae.server.storage.jdbc.internal.UUIDHibernateType"> <column name="ID" length="36" sql-type="CHAR(36)" not-null="true" /> <generator class="assigned" /> </id> 

In this case, this should not be a problem, but how would I do it if such a need arises?

PS: If someone better understands how to handle UUIDs transparently and agnosticize databases, I am grateful.

+2
java uuid hibernate
source share
3 answers

I suggest using the built-in Hibernate UUID generator (see docs ). Then Hibernate should determine the correct matching and size, etc. Column PK.

0
source share

The code returned by UserType.sqlTypes is used in the dialect to search for the registered type. Therefore, if you have sqlTypes, return code that is not yet in use, you can subclass your Dialect and register this code as char (36). For example:

// in your custom type public int [] sqlTypes () {return new int [] {1337}; }

// in your custom dialect registerColumnType (1337, "char (36)")

I have not tried, but it has been reported that it works on the Hibernate forums.

0
source share

You can also map your UUID to BigInteger (java.sql.Types.BIGINT) in a custom type if you are not interested in how your UUID is represented in your database (in which case it will be presented in database 10).

0
source share

All Articles