Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.2.2
Name and version of the database you are using: MySQL 5.0
I am attempting to execute a custom class implementation of an Auxiliary Database Object which first drops a foreign key constraint, then re-adds it w/ON DELETE CASCADE specified.
Pretty simple . . . however, after implementing the
sqlDropString and
sqlCreateString methods, it appears that the DDL specified in sqlDropString is not being executed.
I have tried extending AbstractAuxiliaryDatabaseObject and implementing AuxiliaryDatabaseObject in the custom class, and get the same result. Debug statements prove that the sqlDropString method is being called, just the DDL is not running.
The mapping w/the custom class is below:
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">
<!-- Generated May 10, 2006 2:19:21 PM by Hibernate Tools 3.1.0 beta3 -->
<hibernate-mapping>
<class name="com.xrite.ind.core.tolerances.Tolerance" table="tblTolerance" discriminator-value="Tolerance">
<id name="ID" type="long">
<column name="ToleranceID" />
<generator class="native" />
</id>
<database-object>
<definition class="com.xrite.ind.backcheck.service.auxObjs.ToleranceFKMySQL"/>
<dialect-scope name="org.hibernate.dialect.MySQLInnoDBDialect"/>
</database-object>
</hibernate-mapping>
The mapping that uses the hard coded DDL(which works) is below:
Code:
<database-object>
<create>
alter table tblstandardtolerances drop foreign key FK9E5C4D016AFB6C9A
</create>
<drop></drop>
</database-object>
<database-object>
<create>
alter table tblstandardtolerances add constraint FK9E5C4D016AFB6C9A foreign key(standardID) references tblstandards(standardID) ON DELETE CASCADE
</create>
<drop></drop>
</database-object>
I know string returned by sqlCreateString is being executed because it tries to recreate the constraint and MySQL displays the infamous
Quote:
Can't create table '.\xcolorqc\#sql-310_27.frm' (errno: 121)
message
When I hardcode the DDL as listed above in the mapping, using <create> and <drop>, it works. However, I could only get it to work if I added two create and drop sets, using create to both drop and re-add the constraint. If I coded it in the mapping as below, I get the same errno: 121 message from MySQL. The DTD insists that <create> appear before <drop>:
Code:
<database-object>
<create>
alter table tblstandardtolerances add constraint FK9E5C4D016AFB6C9A foreign key(standardID) references tblstandards(standardID) ON DELETE CASCADE
</create>
<drop>
alter table tblstandardtolerances drop foreign key FK9E5C4D016AFB6C9A
</drop>
</database-object>
Below is the custom class implementation:
Code:
public class ToleranceFKMySQL extends org.hibernate.mapping.AbstractAuxiliaryDatabaseObject {
Collection dialects = new ArrayList();
/** Creates a new instance of ToleranceFKMySQL */
public ToleranceFKMySQL() {
}
public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema)
throws HibernateException
{
System.out.println("inside sqlDropString . . .");
return "alter table tblstandardtolerances drop foreign key FK9E5C4D016AFB6C9A";
}
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema)
throws HibernateException
{
System.out.println("inside sqlCreateString . . .");
return "alter table tblstandardtolerances add constraint FK9E5C4D016AFB6C9A foreign key(standardID) references tblstandards(standardID) ON DELETE CASCADE";
}
}