-->
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: Auxiliary Database Object Custom Class' sqlDropString
PostPosted: Tue Oct 02, 2007 4:14 pm 
Beginner
Beginner

Joined: Mon Sep 05, 2005 4:48 pm
Posts: 31
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";       
    }
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 02, 2007 5:18 pm 
Beginner
Beginner

Joined: Mon Sep 05, 2005 4:48 pm
Posts: 31
I have what is sort of a hack solution now in place whereby I implement the sqlCreateString in two separate custom classes, one to drop the constraint and one to re-add it, and this appears to be working:

Code:
<database-object>
        <definition class="com.xrite.ind.backcheck.service.auxObjs.ToleranceDropFKMySQL"/>         
        <dialect-scope name="org.hibernate.dialect.MySQLInnoDBDialect"/>       
    </database-object>
    <database-object>
        <definition class="com.xrite.ind.backcheck.service.auxObjs.ToleranceRecreateFKMySQL"/>         
        <dialect-scope name="org.hibernate.dialect.MySQLInnoDBDialect"/>       
    </database-object>


Code:
public class ToleranceDropFKMySQL extends org.hibernate.mapping.AbstractAuxiliaryDatabaseObject {
   
    Collection dialects = new ArrayList();
   
    /**
     * Creates a new instance of ToleranceDropFKMySQL
     */
    public ToleranceDropFKMySQL() {     
    }   
   
    public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema)
      throws HibernateException
    {
        //System.out.println("inside sqlDropString . . .");
        //return "alter table tblstandardtolerances drop foreign key FK9E5C4D016AFB6C9A";
        return "";
    }
   
    public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema)
      throws HibernateException
    {
        System.out.println("inside sqlCreateString . . .");
        return "alter table tblstandardtolerances drop foreign key FK9E5C4D016AFB6C9A";       
    }
}


Code:
public class ToleranceRecreateFKMySQL extends org.hibernate.mapping.AbstractAuxiliaryDatabaseObject {
   
    Collection dialects = new ArrayList();
   
    /** Creates a new instance of ToleranceFKMySQL */
    public ToleranceRecreateFKMySQL() {     
    }   
   
    public String sqlDropString(Dialect dialect, String defaultCatalog, String defaultSchema)
      throws HibernateException
    {
        //System.out.println("inside sqlDropString . . .");
        //return "alter table tblstandardtolerances drop foreign key FK9E5C4D016AFB6C9A"; 
        return "";
    }
   
    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";       
    }
}


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.