Hibernate修改命名策略
两步:
第一步:写一个替代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; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。