Pages

Implementing Naming Convention

Implementing Naming Convention

Hibernate has been provided a NamingStrategy interface to apply the naming convention automatically for database table name and column name. 
Let say database table name should be started by "AB_". So you have to create a class that have to extend it  from ImprovedNamingStrategy class and override the following methods.

public class ABNamingStrategy extends ImprovedNamingStrategy {

private static final long serialVersionUID = 1L;

@Override
public String classToTableName(String className) {
return StringHelper.unqualify(className);
}
@Override
public String propertyToColumnName(String propertyName) {
return propertyName;
}
@Override
public String tableName(String tableName) {
return "AB_"+tableName.toUpperCase();
}
@Override
public String columnName(String columnName) {
return columnName;
}

@Override
public String propertyToTableName(String className, String propertyName) {
return "AB_" + classToTableName(className) + "_" +   
                           propertyToColumnName(propertyName);
}
}

classToTableName method will be call when class mapping do not have explicitly table name,  propertyToColumnName method will be call when property element do not have explicitly column name.

Now, you have to do some change in HibernateUtil class,

sessionFactory = new Configuration().setNamingStrategy(new  CENamingStrategy()).configure().buildSessionFactory();   
 


1 comment:

  1. Happy to found this blog. Good Post!. It was so good to read and useful to improve my knowledge as updated one, keep blogging. Hibernate Training in Electronic City
    Java Training in Electronic City

    ReplyDelete