1 package org.hibernate.cfg; 3 4 import java.io.Serializable ; 5 6 import org.hibernate.util.StringHelper; 7 8 14 public class ImprovedNamingStrategy implements NamingStrategy, Serializable { 15 16 19 public static final NamingStrategy INSTANCE = new ImprovedNamingStrategy(); 20 21 protected ImprovedNamingStrategy() {} 22 23 27 public String classToTableName(String className) { 28 return addUnderscores( StringHelper.unqualify(className) ); 29 } 30 34 public String propertyToColumnName(String propertyName) { 35 return addUnderscores(propertyName); 36 } 37 40 public String tableName(String tableName) { 41 return addUnderscores(tableName); 42 } 43 46 public String columnName(String columnName) { 47 return addUnderscores(columnName); 48 } 49 53 public String propertyToTableName(String className, String propertyName) { 54 return classToTableName(className) + '_' + propertyToColumnName(propertyName); 55 } 56 57 private static String addUnderscores(String name) { 58 StringBuffer buf = new StringBuffer ( name.replace('.', '_') ); 59 for (int i=1; i<buf.length()-1; i++) { 60 if ( 61 Character.isLowerCase( buf.charAt(i-1) ) && 62 Character.isUpperCase( buf.charAt(i) ) && 63 Character.isLowerCase( buf.charAt(i+1) ) 64 ) { 65 buf.insert(i++, '_'); 66 } 67 } 68 return buf.toString().toLowerCase(); 69 } 70 71 } 72 | Popular Tags |