I use Hibernate Tools for generating domain code and mapping files.
Here's my scenario:
There's a table 'CODES' with a numeric primary key and another table (CODES_TRANSLATIONS) with a combined primary key (two parts - a numeric and a alphanumeric one).
The first part of the combined key references the primary key of the first table (1:n relationship - CODES=1, CODES_TRANSLATIONS=n).
For better understanding here's the DDL:
Code:
create table codes (sid number, description varchar2(20));
alter table codes
add constraint pk_codes
primary key(sid);
create table codes_translations (sid_codes number, code varchar2(2), text varchar2(20));
alter table codes_translations
add constraint pk_codes_translations
primary key (sid, code);
alter table codes_translations
add constraint fk_codes_sid
foreign key sid_codes
references codes (sid);
In the hibernate.reveng.xml I map the numeric types to java.lang.Long (in the database they have the same type, precision and scale).
In the Hibernate Code Generation dialog I select 'Generate basic typed composite ids'.
Hibernate Tools generates a class Codes, a class CodesTranslations and a class for the combined key - CodesTranslationsId.
Here's the strange thing I discovered:
The field sid in class Codes is of type java.lang.Long, BUT the field sidCodes in class CodesTranslationsId is of primitive type long.
When I deselect 'Generate basic typed composite ids', the class CodesTranslationsId has a reference to Codes.
When I select it again and just have generated the code for CODES_DESCRIPTIONS and NOT for CODES, the field sidCodes is of type java.lang.Long (the behaviour I desired), but, of course, the relationship between the two tables is not mapped either (which is expected behaviour, as I removed the table filter for table CODES).
My question is:
Is it a bug that Hibernate Tools generates a Wrapper Type for the one class and a primitive for another (even if it's mapped)?
Or is Hibernate Tools designed to behave like this - i.e. generating primitive types for Id-Objects?
Is there any possibility to have Hibernate Tools create a java.lang.Long in the Id class instead of primitives?
Is there probably a way to tell Hibernate tools not to generate Id-Objects?