3

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.

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

6 Comments

I'm planning on using it to dynamically create tables in my app. So the user may say I want to create a table with a column named "foo" that will hold strings.
Just infer the type yourself... you know better how to handle your custom types yourself, and don't need a Hibernate function.
Excuse my ignorance but infer how? How would I easily determine what the correct data type is for a specific database? The user could be using any database they wish.
So the user can use any mapping he wants or do you provide the mapping files? In this case use the function I mentioned for basic data types and create the @ManyToOnes for yourself. Maybe you want to use Hibernates schema update function to create the userdefined tables?
There are no mapping files in regards to my problem. The user uses a UI and specifies the name of the table and the types of the columns. They will then hit OK and magically the table will be created with the appropriate columns and data types.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.