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 TaskCode:
<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>