Title: RE: [Hibernate] New NamingStrategy

Great!


-----Original Message-----
From: [EMAIL PROTECTED] on behalf of Emmanuel Bernard
Sent: Wed 11/9/2005 12:19 PM
To: Hibernate development
Subject: [Hibernate] New NamingStrategy

Hi,
For some times now, I've been thinking and implementing a NamingStrategy
that could handle the EJB3 definitions
Here is the result

//$Id: NamingStrategy.java,v 1.1 2004/06/03 16:30:05 steveebersole Exp $
package org.hibernate.cfg;

/**
 * A set of rules for determining the physical column
 * and table names given the information in the mapping
 * document. May be used to implement project-scoped
 * naming standards for database objects.
 *
 * @see DefaultNamingStrategy
 * @see ImprovedNamingStrategy
 * @author Gavin King
 * @author Emmanuel Bernard
 */
public interface NamingStrategy {
    /**
     * Return a table name for an entity class
     * @param className the fully-qualified class name
     * @return a table name
     */
    public String classToTableName(String className);
    /**
     * Return a column name for a property path _expression_
     * @param propertyName a property path
     * @return a column name
     */
    public String propertyToColumnName(String propertyName);
    /**
     * Alter the table name given in the mapping document
     * @param tableName a table name
     * @return a table name
     */
    public String tableName(String tableName);
    /**
     * Alter the column name given in the mapping document
     * @param columnName a column name
     * @return a column name
     */
    public String columnName(String columnName);
    /**
     * Return a table name for a collection
     * @param className the fully-qualified name of the owning entity class
     * @param propertyName a property path
     * @return a table name
     * @deprecated by [EMAIL PROTECTED] #collectionTableName(String, String, String)} ?
     */
    public String propertyToTableName(String className, String
propertyName);
    /**
     * Return a collection table name ie an association having a join table
     *
     * @param ownerEntityTable owner side table name
     * @param associatedEntityTable reverse side table name if any
     * @param propertyName collection role
     */
    public String collectionTableName(String ownerEntityTable, String
associatedEntityTable, String propertyName);
    /**
     * Return the join key column name ie a FK column used in a JOINED
strategy or for a secondary table
     *
     * @param joinedColumn joined column name (logical one) used to join
with
     * @param joinedTable joined table name (ie the referenced table)
used to join with
     */
    public String joinKeyColumnName(String joinedColumn, String
joinedTable);
    /**
     * Return the foreign key column name for the given parameters
     * @param propertyName the property name involved
     * @param propertyTableName the property table name involved
(logical one)
     * @param referencedColumnName the referenced column name involved
(logical one)
     */
    public String foreignKeyColumnName(String propertyName, String
propertyTableName, String referencedColumnName);
    /**
     * Return the logical column name used to refer to a column in the
metadata
     * (like index, unique constraints etc)
     * A full bijection is required between logicalNames and physical ones
     * logicalName have to be case insersitively unique for a given table
     *
     * @param columnName given column name if any
     * @param propertyName property name of this column
     */
    public String logicalColumnName(String columnName, String propertyName);
    /**
     * Returns the logical collection table name used to refer to a
table in the mapping metadata
     *
     * @param tableName the metadata explicit name
     * @param ownerEntityTable owner table entity table name (logical one)
     * @param associatedEntityTable reverse side table name if any
(logical one)
     * @param propertyName collection role
     */
    public String logicalCollectionTablelName(String tableName, String
ownerEntityTable, String associatedEntityTable, String propertyName);

    /**
     * Returns the logical foreign key column name used to refer to this
column in the mapping metadata
     *
     * @param columnName given column name in the metadata if any
     * @param propertyName property name
     * @param referencedColumn referenced column name (logical one) in
the join
     */
    public String logicalCollectionColumnName(String columnName, String
propertyName, String referencedColumn);
}


Here is the EJB3 implementation

//$Id: $
package org.hibernate.cfg;

import org.hibernate.util.StringHelper;
import org.hibernate.AssertionFailure;

/**
 * NAming strategy implementing the EJB3 standards
 *
 * @author Emmanuel Bernard
 */
public class EJB3NamingStrategy implements NamingStrategy {
    public static final NamingStrategy INSTANCE = new EJB3NamingStrategy();

    public String classToTableName(String className) {
        return StringHelper.unqualify(className);
    }

    public String propertyToColumnName(String propertyName) {
        return StringHelper.unqualify(propertyName);
    }

    public String tableName(String tableName) {
        return tableName;
    }

    public String columnName(String columnName) {
        return columnName;
    }

    /** not used in Annotations since deprecated */
    public String propertyToTableName(String className, String
propertyName) {
        return tableName(
                StringHelper.unqualify( className ) + "_" +
StringHelper.unqualify( propertyName )
            );
    }

    public String collectionTableName(String ownerEntityTable, String
associatedEntityTable, String propertyName) {
        return tableName( new StringBuilder(ownerEntityTable).append("_")
                .append(
                    associatedEntityTable != null ?
                    associatedEntityTable :
                    StringHelper.unqualify( propertyName )
                ).toString() );
    }

    public String joinKeyColumnName(String joinedColumn, String
joinedTable) {
        return columnName( joinedColumn );
    }

    public String foreignKeyColumnName(String propertyName, String
propertyTableName, String referencedColumnName) {
        String header = propertyName != null ? propertyName :
propertyTableName;
        if (header == null) throw new AssertionFailure("NammingStrategy
not properly filled");
        return columnName( header + "_" + referencedColumnName );
    }

    public String logicalColumnName(String columnName, String
propertyName) {
        return StringHelper.isNotEmpty( columnName ) ? columnName :
propertyName;
    }

    public String logicalCollectionTablelName(String tableName,
                                              String ownerEntityTable,
String associatedEntityTable, String propertyName
    ) {
        if ( tableName != null ) {
            return tableName;
        }
        else {
            //use of a stringbuffer to workaround a JDK bug
            return new StringBuffer(ownerEntityTable).append("_")
                    .append(
                        associatedEntityTable != null ?
                        associatedEntityTable :
                        StringHelper.unqualify( propertyName )
                    ).toString();
        }
    }

    public String logicalCollectionColumnName(String columnName, String
propertyName, String referencedColumn) {
        return StringHelper.isNotEmpty( columnName ) ? columnName :
propertyName + "_" + referencedColumn;
    }
}




There is the notion of logical name and physical name. The physical name
is obviously the DB name, the logical name is a name that can be used to
refer to the element (column or table) into the metadata (either
annotations or hbm files).
I could have hardcoded this logical rules but it gives flexibility to
make it visible (esp you can used twice the same component class in the
same entity wo having to override any column).

Let me know what you think. I've impelmented annotations to work with
that, I'll check HbmBinder to use it  as much as possible.



-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
hibernate-devel mailing list
hibernate-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to