Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noobthinker/02eb67cc36d0ffbacf8a to your computer and use it in GitHub Desktop.
Save noobthinker/02eb67cc36d0ffbacf8a to your computer and use it in GitHub Desktop.
easy way to make the spring hibernate with camel case underscore. like userName mapping user_name
link:http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/session-configuration.html#configuration-namingstrategy
table sql:
create table camel_case(
id int not null,
user_name varchar(20),
post_message varchar(30)
)
spring bean
@Entity
@Table
class CamelCase{
long id;
String userName;
String postMessage;
}
properties file
hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring xml
<bean id="namingStrategy" class="${hibernate.ejb.naming_strategy}" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/><!-- 1、指定数据源 -->
<property name="namingStrategy" ref="namingStrategy"/>
<property name="packagesToScan">
<list>
<value>com.app</value>
</list>
</property>
<property name="hibernateProperties"><!-- 3、指定Hibernate属性 -->
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>
<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use_reflection_optimizer}</prop>
<!-- -->
<prop key="hibernate.ejb.naming_strategy">${hibernate.ejb.naming_strategy}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop>
<prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop>
</props>
</property>
</bean>
it is work.
#DB
db.driver.class=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=yu
#Hibernate
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
## hibernate camelcase underscore
hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
hibernate.hbm2ddl.auto=none
hibernate.show_sql=false
hibernate.query.substitutions=true 1, false 0
hibernate.default_batch_fetch_size=16
hibernate.max_fetch_depth=2
hibernate.bytecode.use_reflection_optimizer=true
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.EhCacheRegionFactory
net.sf.ehcache.configurationResourceName=/ehcache_hibernate.xml
hibernate.cache.use_structured_entries=true
hibernate.generate_statistics=true
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:resources.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${db.driver.class}" />
<property name="jdbcUrl" value="${db.url}" />
<property name="user" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<bean id="namingStrategy" class="${hibernate.ejb.naming_strategy}" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/><!-- 1、指定数据源 -->
<property name="namingStrategy" ref="namingStrategy"/>
<property name="packagesToScan">
<list>
<value>com.app</value>
</list>
</property>
<property name="hibernateProperties"><!-- 3、指定Hibernate属性 -->
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop>
<prop key="hibernate.default_batch_fetch_size">${hibernate.default_batch_fetch_size}</prop>
<prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>
<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">${hibernate.bytecode.use_reflection_optimizer}</prop>
<!-- -->
<prop key="hibernate.ejb.naming_strategy">${hibernate.ejb.naming_strategy}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="net.sf.ehcache.configurationResourceName">${net.sf.ehcache.configurationResourceName}</prop>
<prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop>
</props>
</property>
</bean>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment