Summary: Database MySQL 4.1. For tables with <joined-sublass> hbm2ddl generates the wrong drop statement . hbm2ddl generates drop constraint clause. The correct one drop foreign key clause.
Propoesed fix is described at the end of E-mail.
Hibernate version:2.1.6
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping
>
<class
name="pol.dao.User"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<id
name="id"
column="id"
type="int"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-User.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="username"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="username"
not-null="true"
unique="true"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-User.xml
containing the additional properties and place it in your merge dir.
-->
<joined-subclass
name="pol.dao.Customer"
dynamic-update="false"
dynamic-insert="false"
>
<key
column="id"
/>
</joined-subclass>
</class>
</hibernate-mapping>
Full log
C:\Java\j2sdk1.4.2_06\bin\java -Dlog4j=D:\PLAY\LEARN\hibernate\src\conf\log4j.properties -Dhibernate.dialect=net.sf.hibernate.dialect.MySQLDialect -Dhibernate.connection.driver_class=com.mysql.jdbc.Driver -Dhibernate.connection.url=jdbc:mysql://localhost:3306/hibernate -Dhibernate.connection.username=root -Dhibernate.connection.password=pavel1812 -Xdebug -Xrunjdwp:transport=dt_socket,address=PavelLaptop:2765,suspend=y,server=n -Dfile.encoding=windows-1252 -classpath C:\Java\j2sdk1.4.2_06\jre\lib\charsets.jar;C:\Java\j2sdk1.4.2_06\jre\lib\jce.jar;C:\Java\j2sdk1.4.2_06\jre\lib\jsse.jar;C:\Java\j2sdk1.4.2_06\jre\lib\plugin.jar;C:\Java\j2sdk1.4.2_06\jre\lib\rt.jar;C:\Java\j2sdk1.4.2_06\jre\lib\sunrsasign.jar;C:\Java\j2sdk1.4.2_06\jre\lib\ext\dnsns.jar;C:\Java\j2sdk1.4.2_06\jre\lib\ext\ldapsec.jar;C:\Java\j2sdk1.4.2_06\jre\lib\ext\localedata.jar;C:\Java\j2sdk1.4.2_06\jre\lib\ext\sunjce_provider.jar;D:\APACHE\hibernate-2.1.6\classes;D:\APACHE\hibernate-2.1.6\lib\commons-logging-1.0.4.jar;D:\APACHE\hibernate-2.1.6\lib\odmg-3.0.jar;D:\APACHE\hibernate-2.1.6\lib\connector.jar;D:\APACHE\hibernate-2.1.6\lib\swarmcache-1.0rc2.jar;D:\APACHE\hibernate-2.1.6\lib\dom4j-1.4.jar;D:\APACHE\hibernate-2.1.6\lib\concurrent-1.3.3.jar;D:\APACHE\hibernate-2.1.6\lib\commons-pool-1.2.jar;D:\APACHE\hibernate-2.1.6\lib\jta.jar;D:\APACHE\hibernate-2.1.6\lib\ehcache-0.9.jar;D:\APACHE\hibernate-2.1.6\lib\commons-collections-2.1.1.jar;D:\APACHE\hibernate-2.1.6\lib\oscache-2.0.jar;D:\APACHE\hibernate-2.1.6\lib\commons-dbcp-1.2.1.jar;D:\APACHE\hibernate-2.1.6\lib\commons-lang-1.0.1.jar;D:\APACHE\hibernate-2.1.6\lib\cglib-full-2.0.2.jar;D:\APACHE\hibernate-2.1.6\lib\proxool-0.8.3.jar;D:\APACHE\hibernate-2.1.6\lib\c3p0-0.8.4.5.jar;D:\APACHE\hibernate-2.1.6\lib\jboss-system.jar;D:\APACHE\hibernate-2.1.6\lib\jgroups-2.2.3.jar;D:\APACHE\hibernate-2.1.6\lib\jcs-1.0-dev.jar;D:\APACHE\hibernate-2.1.6\lib\jboss-common.jar;D:\APACHE\hibernate-2.1.6\lib\junit-3.8.1.jar;D:\APACHE\hibernate-2.1.6\lib\jboss-cache.jar;D:\APACHE\hibernate-2.1.6\lib\jboss-jmx.jar;D:\APACHE\hibernate-2.1.6\lib\jaas.jar;D:\APACHE\hibernate-2.1.6\lib\jdbc2_0-stdext.jar;D:\APACHE\hibernate-2.1.6\lib\log4j-1.2.8.jar;C:\Java\IntelliJ-IDEA-4.5.2\lib\ant\lib\ant.jar;D:\PLAY\LEARN\hibernate\target\classes;C:\Java\MySQL\mysql-connector-java-3.0.15-ga-bin.jar;C:\Java\IntelliJ-IDEA-4.5.2\lib\idea_rt.jar net.sf.hibernate.tool.hbm2ddl.SchemaExport --properties=schema.hibernate.properties --output=create-tables.sql pol\dao\Category.hbm.xml pol\dao\User.hbm.xml pol\dao\Charge.hbm.xml
10:14:43,734 INFO Environment:469 - Hibernate 2.1.6
10:14:43,765 INFO Environment:498 - hibernate.properties not found
10:14:43,765 INFO Environment:529 - using CGLIB reflection optimizer
10:14:43,781 INFO Configuration:169 - Mapping file: pol\dao\Category.hbm.xml
10:14:44,093 WARN XMLHelper:55 - Warning parsing XML: pol\dao\Category.hbm.xml(1) Declared encoding "UTF-8" does not match actual one "Cp1252"; this might not be an error.
10:14:53,203 INFO Binder:229 - Mapping class: pol.dao.Category -> Category
10:14:53,484 INFO Configuration:169 - Mapping file: pol\dao\User.hbm.xml
10:14:53,500 WARN XMLHelper:55 - Warning parsing XML: pol\dao\User.hbm.xml(1) Declared encoding "UTF-8" does not match actual one "Cp1252"; this might not be an error.
10:15:10,203 INFO Binder:229 - Mapping class: pol.dao.User -> User
10:15:10,281 INFO Binder:200 - Mapping joined-subclass: pol.dao.Customer -> Customer
10:15:10,281 INFO Configuration:169 - Mapping file: pol\dao\Charge.hbm.xml
10:15:10,296 WARN XMLHelper:55 - Warning parsing XML: pol\dao\Charge.hbm.xml(1) Declared encoding "UTF-8" does not match actual one "Cp1252"; this might not be an error.
10:15:11,406 INFO Binder:229 - Mapping class: pol.dao.Charge -> Charge
10:15:11,453 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.MySQLDialect
10:15:11,453 INFO Configuration:627 - processing one-to-many association mappings
10:15:11,453 INFO Configuration:636 - processing one-to-one association property references
10:15:11,453 INFO Configuration:661 - processing foreign key constraints
10:18:17,359 INFO Configuration:627 - processing one-to-many association mappings
10:18:17,359 INFO Configuration:636 - processing one-to-one association property references
10:18:17,359 INFO Configuration:661 - processing foreign key constraints
10:18:17,390 INFO SchemaExport:98 - Running hbm2ddl schema export
10:18:17,390 INFO SchemaExport:112 - writing generated schema to file: create-tables.sql
10:18:17,390 INFO SchemaExport:117 - exporting generated schema to database
10:18:17,406 INFO DriverManagerConnectionProvider:42 - Using Hibernate built-in connection pool (not for production use!)
10:18:17,406 INFO DriverManagerConnectionProvider:43 - Hibernate connection pool size: 20
10:18:17,437 INFO DriverManagerConnectionProvider:77 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/hibernate
10:18:17,437 INFO DriverManagerConnectionProvider:78 - connection properties: {user=root, password=pavel1812}
alter table Customer drop constraint FK27FBE3FED1B
10:18:17,843 DEBUG SchemaExport:132 - alter table Customer drop constraint FK27FBE3FED1B
10:18:17,859 DEBUG SchemaExport:137 - Unsuccessful: alter table Customer drop constraint FK27FBE3FED1B
10:18:17,859 DEBUG SchemaExport:138 - Syntax error or access violation message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'constraint FK27FBE3FED1B' at line 1" drop table if exists Charge .......
Full stack trace of any exception that occurs:
Name and version of the database you are using: MySql 4.1
The generated SQL (show_sql=true):
See log above
The correct statement should be
alter table Customer drop foreign key FK27FBE3FED1B;
Debug level Hibernate log excerpt: N/A
Proposed code change
The generated sting can not be customized through dialect. The solution which works for MySQL 4.1 (I did not checked against other databases) is to add the following code to class net.sf.hibernate.mapping.ForeignKey overwriting sqlDropString from Constraint class.
public String sqlDropString(Dialect dialect) { return "alter table " + getTable().getQualifiedName(dialect) + " drop foreign key " + getName(); }
|