Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Hibernate to find the specific database type for a given Java type

Is there a class that I could use in Hibernate so that I could pass in a class and it would return the appropriate database type name?

For example,

String type = HibernateTypeGod.getTypeName(String.class);

Hibernate would determine the dialect I specified and return me VARCHAR2 in the case of Oracle.

like image 391
digiarnie Avatar asked Apr 24 '26 07:04

digiarnie


1 Answers

It's not that easy. All the mapping stuff is in the classes Configuration, Table, Column and Dialect, so you will need some kind of valid config to query this from Hibernate. But you can for example use:

String s = myDialect.getTypeName(sqlType,length,precision,scale);

Of course you need to find the dialect and the sqlType, but this isn't fixed in Hibernate on a class basis, but can be configured per column anyway, so what is the reason to ask by Class and not by column name?

like image 118
Daniel Avatar answered Apr 26 '26 20:04

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!