Problem description:
I’m developing a Java+Hibernate program using MySql as database. The program should connect to some
existing tables created outside Hibernate. One of these tables is
clients having the following fields:
- id - integer
unsigned (!!!!)
- name - varchar(255)
For this table my program will have the following mapping file (as you see integer unsigned will be mapped to an integer. What else?):
Code:
<hibernate-mapping>
<class name="com.mycompany.Clients" table="clients" >
<id name="id"
column="ID
type="integer"
access="property">
<generator class="native"/>
</id>
<property name="name" column="name" type="string" />
</class>
</hibernate-mapping>
My Hibernate program should create a table, let’s call it
data, having a FK column pointing to clients table:
- client_id_FK
- domain
- info1
(client_id_FK , domain) is the primary key.
The mapping file will be:
Code:
<hibernate-mapping>
<class name="com.mycompany.Data" table="data">
<composite-id>
<key-many-to-one name="clientId" column="client_id_FK" class="com.mycompany.Clients" />
<key-property name="domain" column="domain" type="string" />
</composite-id>
<property name="info1" column="info1" type="long"/>
</class>
</hibernate-mapping>
The problem appears when program begins and creates the tables. The error is:
Unsuccessful: alter table data add index FK1A5B33BD866FFDB (client_id_FK), add constraint FK1A5B33BD866FFDB foreign key (client_id_FK) references clients (ID)
Can't create table '.\schema1\#sql-dac_7.frm' (errno: 150)
After my investigations I concluded that the cause of the problem is the fact that Hibernate creates data.client_id_FK column as integer and not as integer unsigned. Because the id field in clients table is declared as integer unsigned, MySql cannot create the foreign key constraint (even in query browser editor where I receive the same error).
After I’ve manually changed data.client_id_FK to integer unsigned the program worked fine and automatically created the FK to clients table and the index.
My questions:
Is it any possibility to force Hibernate to automatically create data.client_id_FK as integer unsigned?
If this isn’t possible then how should be solved this kind of problems without manual intervention?