Sponsered Links
Categories
Sponsered Links

NamingStrategy implementation

 

The classToTableName() method is called only if a <class> mapping doesn’t specify an explicit table name. The propertyToColumnName() method is called if a property has no explicit column name. The tableName() and columnName() methods are called when an explicit name is declared.
If we enable our CENamingStrategy, this class mapping declaration <class name="BankAccount"> will result in CE_BANKACCOUNT as the name of the table. The classToTableName() method was called with the fully qualified class name as the argument.
However, if a table name is specified <class name="BankAccount" table="BANK_ACCOUNT"> then CE_BANK_ACCOUNT will be the name of the table. In this case, BANK_ACCOUNT was passed to the tableName() method.
The best feature of the NamingStrategy is the potential for dynamic behavior. To activate a specific naming strategy, we can pass an instance to the Hibernate Configuration at runtime:
Configuration cfg = new Configuration();
cfg.setNamingStrategy( new CENamingStrategy() );
SessionFactory sessionFactory = cfg.configure().buildSessionFactory();
This will allow us to have multiple SessionFactory instances based on the same mapping documents, each using a different NamingStrategy. This is extremely useful in a multiclient installation where unique table names (but the same data model) are required for each client.

public class CENamingStrategy implements NamingStrategy {
public String classToTableName(String className) {
return tableName(
StringHelper.unqualify(className).toUpperCase() );
}
public String propertyToColumnName(String propertyName) {
return propertyName.toUpperCase();
}
public String tableName(String tableName) {
return "CE_" + tableName;
}
public String columnName(String columnName) {
return columnName;
}
public String propertyToTableName(String className,
String propertyName) {
return classToTableName(className) + '_' +
propertyToColumnName(propertyName);
}
}

 
 
Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved