Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change PrimaryKey type in model classes of Oracle db, generated with hibernate

I have a database on Oracle 11 XE and all my tables have primary keys that are NUMERIC(22,0). I have generated model classes with Hibernate tools in Eclipse. Unfortunately, all NUMERIC fields have been implemented as BigDecimals. Even fields representing INT columns are changed too. I tried some reverse engineering, but it seems not to work in my case.

How can I fix it?

like image 737
xenteros Avatar asked Oct 30 '22 23:10

xenteros


2 Answers

You better use BIGINT instead of NUMERIC(22,0) for your IDs. Then re-generate the entities. The Java-IDs should be of type Long. If not you have to change the type manually. In general you should see generated entities more then a template which needs some modifications.

Reference: Mapping SQL and Java Types

like image 85
Kai Avatar answered Nov 13 '22 04:11

Kai


You can customize the type mapping in Hibernate tools. Specify the mappings in the reveng.xml. Translate every JDBC type to a Hibernate type and run your generator again. E.g.

 <sql-type>jdbc-type="NUMERIC" precision="22" hibernate-type="Long"</sql-type>

see also: http://omtlab.com/java-hibernate-reverse-engineering-eclipse-tutorial/

like image 32
wgitscht Avatar answered Nov 13 '22 02:11

wgitscht