-->
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.  [ 5 posts ] 
Author Message
 Post subject: SchemaExportTask problem with MySQL InnoDB
PostPosted: Tue Jun 21, 2005 11:43 pm 
Newbie

Joined: Tue Jun 21, 2005 5:44 pm
Posts: 6
Location: Cd. Madero, México
Hi All,

I'm a having a problem with the generated sql code of hbm2ddl using MySQL InnoDB. I'm trying to create a very simple Country-State relationship with these restrictions:

* Every country must have a unique country-code (mx, uk, jp, etc.)
* Every State must be related to an existing Country in the DB

I think my mappings are correct but anyway, I'm getting an extra Foreign Key in the States table that's preventing me from inserting correctly:

alter table sv_state add index FKB4ED9FB5349A1B28 (state_id), add constraint FKB4ED9FB5349A1B28 foreign key (state_id) references sv_country (country_id);

After I remove that line everything works as expected. Is there any workaround for this kind of situation? What am I doing wrong?

Thanks! Details Below:


Hibernate version: 3.02

Mapping documents:
hibernate.cfg.xml
Code:
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- properties -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/etoys</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">pass</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <property name="hibernate.connection.pool_size">10</property>
        <property name="hibernate.show_sql">true</property>       
       
        <!-- mapping files -->
        <mapping resource="com/supervaca/etoys/bll/Country.hbm.xml"/>
        <mapping resource="com/supervaca/etoys/bll/State.hbm.xml"/>
    </session-factory>   
</hibernate-configuration>


Country.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.supervaca.etoys.bll">
    <class name="Country" table="sv_country">
        <id name="id" column="country_id" type="long">
            <generator class="native"/>
        </id>       
       
        <property name="name" column="country_name" not-null="true" length="60" type="string"/>
       
   <!-- business key -->
        <property name="code" column="country_code" not-null="true" length="2" type="string" unique="true"/>
       
        <set
            name="states"
            table="sv_state"
            cascade="all-delete-orphan"           
            inverse="true">
            <key column="state_id" />
            <one-to-many class="State" />
        </set>       
    </class>
</hibernate-mapping>


State.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.supervaca.etoys.bll">
    <class name="State" table="sv_state">
        <id name="id" column="state_id" type="long">
            <generator class="native"/>
        </id>
       
        <property name="name" column="state_name" not-null="true" length="60" type="string"/>
       
        <!--
        <set
            name="cities"
            table="sv_city"
            cascade="save-update"           
            inverse="true">
            <key column="city_id" />
            <one-to-many class="City" />
        </set>
        -->
       
        <many-to-one
            name="country"
            class="Country"
            column="country_id"
            cascade="none"
            not-null="true"                     
        />
    </class>
</hibernate-mapping>


Name and version of the database you are using: MySQL 4.1.11-nt

The generated SQL (show_sql=true):
Code:
alter table sv_state drop foreign key FKB4ED9FB5A962EE03;
alter table sv_state drop foreign key FKB4ED9FB5349A1B28;
drop table if exists sv_country;
drop table if exists sv_state;
create table sv_country (
   country_id bigint not null auto_increment,
   country_name varchar(60) not null,
   country_code varchar(2) not null unique,
   primary key (country_id)
) type=InnoDB;
create table sv_state (
   state_id bigint not null auto_increment,
   state_name varchar(60) not null,
   country_id bigint not null,
   primary key (state_id)
) type=InnoDB;
alter table sv_state add index FKB4ED9FB5A962EE03 (country_id), add constraint FKB4ED9FB5A962EE03 foreign key (country_id) references sv_country (country_id);
alter table sv_state add index FKB4ED9FB5349A1B28 (state_id), add constraint FKB4ED9FB5349A1B28 foreign key (state_id) references sv_country (country_id);


Ant Task
Code:
    <target name="schemaexport" depends="init">
        <taskdef name="schemaexport"
            classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
            classpath="${run.classpath}"
        />
        <schemaexport
            config="${basedir}/runtime/hibernate.cfg.xml"
            quiet="no"
            text="no" drop="no"
            delimiter=";"
            output="schema-export.sql">
            <fileset dir="src">
                <include name="**/*.hbm.xml"/>
            </fileset>
        </schemaexport>
    </target>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 11:59 pm 
Newbie

Joined: Tue Jun 21, 2005 5:44 pm
Posts: 6
Location: Cd. Madero, México
I upgraded to Hibernate 3.05 and used other MySQL Dialects and still got the same problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 12:39 pm 
Newbie

Joined: Tue Jun 21, 2005 5:44 pm
Posts: 6
Location: Cd. Madero, México
Could someone try this same mapping with other DBMS like MS-SQL or Postgre to see if you have the same issue using Hibernate 3?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 1:50 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Your set association is wrong on your country class. The key column needs to be the column in the associated table that references the country class. Change it to be:

Code:
        <set
            name="states"
            table="sv_state"
            cascade="all-delete-orphan"           
            inverse="true">
            <key column="country_id" />
            <one-to-many class="State" />
        </set> 


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 2:05 am 
Newbie

Joined: Tue Jun 21, 2005 5:44 pm
Posts: 6
Location: Cd. Madero, México
Thanks a lot man! That fixed all the problems, you made my day! :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.