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_" />
<property name="columnPrefix" value="crm_" />
</bean>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
${hibernate.dialect}
</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
......
</list>
</property>
<property name="annotatedPackages">
<list>
......
</list>
</property>
</bean>
{code}
相关推荐
自动生成hibernate映射文件和实体类
关于hibernate映射和查询,使用MySQL,适合初学者的练习
hibernate映射枚举类型,hibernate如何映射枚举类型
根据实体类生成简单的Hibernate映射文件,对不用Myeclipse的童鞋还是有用
build.xml 手写自动生成Hibernate映射文件和配置文件源代码
用来生成hibernate映射文件.从而实现不需手动写入。。。很实用的东东哦。。
常用 Hibernate 映射配置说明.doc
hibernate实体映射文件字段设置默认值
下面小编就为大家带来一篇spring hibernate实现动态替换表名(分表)的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
myeclipse自动生成hibernate映射文件
Hibernate映射解析 七种映射关系
用Hibernate映射继承关系
Myeclipse生成Hibernate映射文件
Hibernate 映射类型与Java 类型的对照表
用MyEclipse自动生成hibernate映射文件和实体类
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate关联映射的...
Hibernate映射配置实例大全:详细总结了Hibernate的各种映射配置的方法,并逐一举例
Hibernate映射集合属性List,用于映射集合属性 List Hibernate 自动生成表 不需sql
hibernate 映射关系学习入门 多对多实体映射 源码