>
两步:
第一步:写一个替代Hibernate默认命名规则类(本类目标是把java【骆驼命名】 转换成 【下划线命名】)
package paltform.init; import org.hibernate.cfg.ImprovedNamingStrategy; import org.hibernate.cfg.NamingStrategy; /** * HIBERNATE 数据库字段命名规则 * */ public class HibernateNamingStrategy extends ImprovedNamingStrategy implements NamingStrategy { private static final long serialVersionUID = 1L; @Override public String columnName(String columnName) { return addUnderscores(columnName).toUpperCase(); } @Override public String tableName(String tableName) { return addUnderscores(tableName).toUpperCase(); } }
第二:创建SessionFactory设置一下
package paltform.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import paltform.init.HibernateNamingStrategy; public class UtilHibernate { private static final String HB_CONFIG_PATH = "platform/hibernate.cfg.xml"; private static SessionFactory sessionFactory = null; /** * Get session factory * ----------------------- * @return Return sessionFactory * */ public synchronized static SessionFactory getSessionFactory(){ if(sessionFactory == null){ Configuration cfg = new Configuration().configure(HB_CONFIG_PATH); cfg.setNamingStrategy(new HibernateNamingStrategy()); // 修改HIBERNATE命名规范 StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()); StandardServiceRegistry sr = srb.build(); sessionFactory = cfg.buildSessionFactory(sr); } return sessionFactory; } }
Hibernate修改命名策略
时间: 2024-10-30 14:57:54