package cn.partner4java.shop.bean;
import org.hibernate.cfg.ImprovedNamingStrategy;
/**
* 使每个表前面加上"PA_"
* @author partner4java
*
*/
public class PANamingStrategy extends ImprovedNamingStrategy {
/**
* 当没有声明显示的名称时调用
*/
@Override
public String classToTableName(String className) {
return "PA_"+ super.classToTableName(className);
}
/**
* 当声明了显示的名称时调用
* Alter the table name given in the mapping document
*/
@Override
public String tableName(String tableName) {
return "PA_"+ super.tableName(tableName);
}
}
package cn.partner4java.shop.service;
import org.hibernate.cfg.Configuration;
import cn.partner4java.shop.bean.PANamingStrategy;
public class BaseTest {
public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.setNamingStrategy(new PANamingStrategy());
configuration.configure().buildSessionFactory();
}
}
配置:<class name="BankAccount"> 生成表明:pa_bank_account
配置:<class name="BankAccount" table="BankAccount"> 生成表明:pa_bank_account (书中说的是错误的)
配置:<class name="BankAccount" table="BANK_ACCOUNT"> 生成表明:pa_bank_account