-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to force DB column type to integer unsigned ?
PostPosted: Fri Jan 12, 2007 11:28 am 
Newbie

Joined: Fri Jan 12, 2007 11:08 am
Posts: 2
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 15, 2007 2:13 pm 
Newbie

Joined: Fri Jan 12, 2007 11:08 am
Posts: 2
This solved my problem....

<key-many-to-one name="clientId" class="com.mycompany.Clients">
<column name="client_id_FK" sql-type="integer unsigned"> </column>
</key-many-to-one>

I'm surpised that nobody has found it - it was not so difficult......


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.