facebook

Hibernate Reverse Engineering Templates and MySQL backtics

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #291533 Reply

    dbennett455
    Participant

    I would like to modify the Hibernate Reverse Engineering templates to backtick all table and column references to fix compatibility issues between Hibernate and MySQL. I am using Hibernate 3.2 Annotations so I would like to change the Abstract data object POJO’s generation from:

    
        @Column(name = "key", nullable = false, length = 2)
        public String getKey() {
            return this.key;
        }
    

    To:

    
        // backticks around column name
        @Column(name = "`key`", nullable = false, length = 2)
        public String getKey() {
            return this.key;
        }
    

    I have copied the velocity templates from com.genuitec.eclipse.hibernate_6.6.0.zmyeclipse660200810/templates into the project, but I can’t see where the annotation code is exposed in the templates.

    Granted this should be handled by Hibernate like all of the MySQL utilities do, however, the Hibernate team keeps closing the issue whenever anyone brings this up saying ‘add the backticks to your mappings’. This would work, except in the case of reverse engineering where you are overwriting your mappings and you loose the backticks.

    Is is possible to modify Hibernate Reverse Engineering to do this?

    #291534 Reply

    dbennett455
    Participant

    Assuming this is going to be difficult, I wrote a perl script to do the work. This will edit the source files in place and only update table, catalog and column references that are not already backticked. (i.e. it’s safe to run multiple times)

    
    #!/usr/bin/perl -w
    use strict;
    
    #fixup MyEclipse IDE reversed enginered hibernate 3.2 annotated objects 
    
    use Cwd;
    
    my $dbDir='{insert directory to your generated objects here}';;
    
    chdir($dbDir);
    
    # Backquote table and catalog names
    system 'perl -pi.orig -e \'s/(\@Table.*name = ")([^`][^"]*)(", catalog = ")([^`][^"]*)(")/$1`$2`$3`$4`$5/g\' *.java';
    # Backquote column names
    system 'perl -pi.orig -e \'s/(\@Column.*name = ")([^`][^"]*)(")/$1`$2`$3/g\' *.java';
    # Remove backup files
    system 'rm -r *.orig';
    
    #291566 Reply

    dbennett455
    Participant

    After reading this section in the documentation:

    http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping-quotedidentifiers.html

    It appears that the backtick quotes are treated in a cross-platform way by Hibernate. I would recommend that quoting identifiers be added as a feature to the MyEclipse Hibernate Reverse engineering tool.

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: Hibernate Reverse Engineering Templates and MySQL backtics

You must be logged in to post in the forum log in