-->
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.  [ 5 posts ] 
Author Message
 Post subject: Naming Strategy: DB Table to Class
PostPosted: Fri Jan 18, 2008 8:30 pm 
Beginner
Beginner

Joined: Thu Jan 17, 2008 8:58 pm
Posts: 20
I am using the Hibernate generation tools in MyEclipse to generate the Java classes, hbm.xml, etc from my DB tables.

In the Hibernate book it talks about custom naming strategies and has a classToTableName() method.

Since I'm generating the class from the table, I need the equivalent of the inverse method: tableToClassName().

I want my tables to have a prefix (e.g. R_USER) but the class should not have the prefix (e.g. User class).

Seems like I would need some config spec in hibernate.cfg.xml or a reverse-engineering file.

Is there some way to do this?

Thanks,

_________________
-- Frank


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 19, 2008 6:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I don't know about what myeclipse supports, but I do know that in Hibernate Tools you just use a reveng.xml or set a Custom reverseengineeringstrategy - see the docs on tools.hibernate.org for that.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 19, 2008 2:24 pm 
Beginner
Beginner

Joined: Thu Jan 17, 2008 8:58 pm
Posts: 20
I was able to find some documentation in the MyEclipse helps which pointed me in the right direction for the MyEclipse reverse engineering tools.

From my brief look at the documentation on the hibernae site, the MyEclipse support is different.

In MyEclipse look at the section titled "MyEclipse Hibernate Quickstart" and see the section "Using templates to fine tune generated code".

_________________
-- Frank


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 20, 2008 12:30 am 
Beginner
Beginner

Joined: Thu Jan 17, 2008 8:58 pm
Posts: 20
OK, I've figured out how to do this in MyEclipse.

There are two facilities:

- templates are used to control the code generated for the various java and hbm.xml files.

- DelegatingReverseEngineeringStrategy is a java class used to define a lot of the mapping of various DB names and properties to the ones to be used in the java classes.

For my task, to remove the "R_" prefix from the DB tables, I needed to us the second one.

You might think there would be one method to define this, but there are different methods for the different places where the table/class name is used - in the class name definition, as fiels types, as parameter types, etc.

Here is the actual class I created. Works ok for my situation, but you might find you need to implement some of the other methods:

Code:
import java.util.List;

import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.TableIdentifier;
import org.hibernate.mapping.ForeignKey;

public class RevEngStrategy extends DelegatingReverseEngineeringStrategy {

    public RevEngStrategy(ReverseEngineeringStrategy strategy) {
            super(strategy);
    }

    @Override
    public String foreignKeyToCollectionName(String keyname,
                    TableIdentifier fromTable, List fromColumns,
                    TableIdentifier referencedTable, List referencedColumns,
                    boolean uniqueReference) {
            fromTable = removePrefix(fromTable);
            referencedTable = removePrefix(referencedTable);
            return super.foreignKeyToCollectionName(keyname, fromTable, fromColumns,
                            referencedTable, referencedColumns, uniqueReference);
    }

    @Override
    public String foreignKeyToEntityName(String keyname,
                    TableIdentifier fromTable, List fromColumnNames,
                    TableIdentifier referencedTable, List referencedColumnNames,
                    boolean uniqueReference) {
            fromTable = removePrefix(fromTable);
            referencedTable = removePrefix(referencedTable);
            return super.foreignKeyToEntityName(keyname, fromTable, fromColumnNames,
                            referencedTable, referencedColumnNames, uniqueReference);
    }

    @Override
    public String foreignKeyToManyToManyName(ForeignKey fromKey,
                    TableIdentifier middleTable, ForeignKey toKey,
                    boolean uniqueReference) {
            middleTable = removePrefix(middleTable);
            // TODO Auto-generated method stub
            return super.foreignKeyToManyToManyName(fromKey, middleTable, toKey,
                            uniqueReference);
    }

    @Override
    public String tableToCompositeIdName(TableIdentifier identifier) {
            identifier = removePrefix(identifier);
            return super.tableToCompositeIdName(identifier);
    }

    @Override
    public String tableToIdentifierPropertyName(TableIdentifier tableIdentifier) {
            tableIdentifier = removePrefix(tableIdentifier);
            return super.tableToIdentifierPropertyName(tableIdentifier);
    }

    @Override
    public String tableToClassName(TableIdentifier tableIdentifier) {
            tableIdentifier = removePrefix(tableIdentifier);
            return super.tableToClassName(tableIdentifier);
    }

   
   
   
    private TableIdentifier removePrefix(TableIdentifier tableIdentifier) {
            String name = tableIdentifier.getName();
            if (name.toUpperCase().startsWith("R_")) {
                    tableIdentifier = new TableIdentifier(tableIdentifier.getSchema(), tableIdentifier.getCatalog(), name.substring(2));
            }
            return tableIdentifier;
    }

}


_________________
-- Frank


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 20, 2008 4:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
this is exactly the same...because myeclipse reverse engineering is using a forked version of hibernate tools...

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.