hibernate映射时表名与列名前缀的设置

h2.hibernate映射时对于cofing的一些扩展
h3.改变表名与列名的映射
  继承NamingStrategy接口,实现其中的表名与列名命名函数,完成自己的命名策略设定。当然可以根据实际需要做其他扩展。下面的例子仿造hibernate的DefaultNamingStrategy和ImprovedNamingStrategy实现的一个命名策略。可以设置的字段在类代码中的前几个属性,有注释说明。这个策略不影响用标注已经申明的table和column的标注命名(及标注命名优先级高)。
{code}
package com.test.commos;
import org.hibernate.AssertionFailure;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.util.StringHelper;
/**
 * 仿造DefaultNamingStrategy实现一个自己的table和column的整体命名策略。
 * 
 * 
 */
public class LocalNamingStrategy implements NamingStrategy {
 /**
  * 设置表名前缀。
  */
 private String _tablePrefix = "";
 /**
  * 设置列名前缀。
  */
 private String _columnPrefix = "";
 /**
  * 设置是否转换className与propertyName为分词后加下划线的形式。如:AaaBbb->aaa_bbb;
  * aaaBbb->aaa_bbb。
  */
 private boolean _addUnderScores = false;
 public void setTablePrefix(String tablePrefix) {
  if (StringHelper.isNotEmpty(tablePrefix))
   _tablePrefix = tablePrefix;
 }
 public void setColumnPrefix(String columnPrefix) {
  if (StringHelper.isNotEmpty(columnPrefix))
   _columnPrefix = columnPrefix;
 }
 public void setAddUnderScores(boolean addUnderScores) {
  _addUnderScores = addUnderScores;
 }
 /**
  * A convenient singleton instance
  */
 public static final NamingStrategy INSTANCE = new LocalNamingStrategy();
 /**
  * Return the unqualified class name, mixed case converted to underscores
  */
 public String classToTableName(String className) {
  return addPrefixes(StringHelper.unqualify(className), _tablePrefix);
 }
 /**
  * Return the full property path with underscore seperators, mixed case
  * converted to underscores
  */
 public String propertyToColumnName(String propertyName) {
  return addPrefixes(StringHelper.unqualify(propertyName), _columnPrefix);
 }
 /**
  * Convert mixed case to underscores
  */
 public String tableName(String tableName) {
  return addPrefixes(tableName, "");
 }
 /**
  * Convert mixed case to underscores
  */
 public String columnName(String columnName) {
  return addPrefixes(columnName, "");
 }
 private String addPrefixes(String name, String prefix) {
  StringBuffer buffer = new StringBuffer();
  buffer.append(prefix);
  if (_addUnderScores) {
   buffer.append(addUnderscores(name));
  } else {
   buffer.append(name);
  }
  return buffer.toString();
 }
 private String addUnderscores(String name) {
  StringBuffer buf = new StringBuffer(name.replace('.', '_'));
  for (int i = 1; i < buf.length() - 1; i++) {
   if (Character.isLowerCase(buf.charAt(i - 1))
     && Character.isUpperCase(buf.charAt(i))
     && Character.isLowerCase(buf.charAt(i + 1))) {
    buf.insert(i++, '_');
   }
  }
  return buf.toString().toLowerCase();
 }
 public String collectionTableName(String ownerEntityTable,
   String associatedEntityTable, String propertyName) {
  return tableName(ownerEntityTable + '_' + propertyName);
 }
 /**
  * Return the argument
  */
 public String joinKeyColumnName(String joinedColumn, String joinedTable) {
  return columnName(joinedColumn);
 }
 /**
  * Return the property name or propertyTableName
  */
 public String foreignKeyColumnName(String propertyName,
   String propertyTableName, String referencedColumnName) {
  String header = propertyName != null ? StringHelper
    .unqualify(propertyName) : propertyTableName;
  if (header == null)
   throw new AssertionFailure("NammingStrategy not properly filled");
  return columnName(header + "_" + referencedColumnName); // + "_" +
  // referencedColumnName
  // not used for
  // backward compatibility
 }
 /**
  * Return the column name or the unqualified property name
  */
 public String logicalColumnName(String columnName, String propertyName) {
  return StringHelper.isNotEmpty(columnName) ? columnName : StringHelper
    .unqualify(propertyName);
 }
 /**
  * Returns either the table name if explicit or if there is an associated
  * table, the concatenation of owner entity table and associated table
  * otherwise the concatenation of owner entity table and the unqualified
  * property name
  */
 public String logicalCollectionTableName(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();
  }
 }
 /**
  * Return the column name if explicit or the concatenation of the property
  * name and the referenced column
  */
 public String logicalCollectionColumnName(String columnName,
   String propertyName, String referencedColumn) {
  return StringHelper.isNotEmpty(columnName) ? columnName : StringHelper
    .unqualify(propertyName)
    + "_" + referencedColumn;
 }
}
{code}
h3.在spring的配置中加入命名策略的申明。
{code}
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="namingStrategy">
   <bean
    class="com.test.commons.LocalNamingStrategy">
    <property name="tablePrefix" value="crm_" /
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值