日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hibernate修改命名策略

發布時間:2025/4/16 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hibernate修改命名策略 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

解決思路:

1、解決關鍵字問題;

2、解決大小寫敏感的問題。

首先解決第一個問題,這里有幾種方式

(1)將表名或字段名用方括號([])括起來

XML

<property name="context" type="string" > <column name="[CONTEXT]" length="255" not-null="true" /> </property>

注解

@Column(name = "[CONTEXT]", nullable = false) private String context;

(2)將表名或字段名用雙引號(")括起來

XML

<property name="context" type="string" > <column name='"CONTEXT"' length="255" not-null="true" /> </property>

注解

@Column(name = "\"CONTEXT\"", nullable = false) private String context;

????解決了第一個問題之后我們再來解決第二個問題,由于Hibernate默認講所有表名、字段名都轉換成小寫字母,所以經過轉換之后的SQL語句類似于SELECT obj."context"?FROM obj;這樣的SQL在Oracle中運行也有問題,而SELECT OBJ."CONTEXT"?FROM OBJ;這樣的SQL運行就問題,所以就需要解決Hibernate大小寫的問題,Hibernate控制表名和字段名大小寫的配置類為org.hibernate.cfg.ImprovedNamingStrategy,所以我們現在要重寫Hibernate的命名策略。

? ? 總共需要兩個步驟:

(1)寫一個替代Hibernate默認命名規則類

package com.xjj.framework.hibernate.cfg;import java.io.Serializable; import java.util.Locale;import org.hibernate.AssertionFailure; import org.hibernate.cfg.ImprovedNamingStrategy; import org.hibernate.cfg.NamingStrategy; import org.hibernate.internal.util.StringHelper;public class DMNamingStrategy extends ImprovedNamingStrategy implementsNamingStrategy, Serializable {/*** A convenient singleton instance*/public static final NamingStrategy INSTANCE = new DMNamingStrategy();/*** Return the unqualified class name, mixed case converted to* underscores*/@Overridepublic String classToTableName(String className) {return addUnderscores( StringHelper.unqualify(className) );}/*** Return the full property path with underscore seperators, mixed* case converted to underscores*/@Overridepublic String propertyToColumnName(String propertyName) {return addUnderscores( StringHelper.unqualify(propertyName) );}/*** Convert mixed case to underscores*/@Overridepublic String tableName(String tableName) {return addUnderscores(tableName);}/*** Convert mixed case to underscores*/@Overridepublic String columnName(String columnName) {return addUnderscores(columnName);}protected static String addUnderscores(String name) {StringBuilder buf = new StringBuilder( 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().toUpperCase(Locale.ROOT);}@Overridepublic String collectionTableName(String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable,String propertyName) {return tableName( ownerEntityTable + '_' + propertyToColumnName(propertyName) );}/*** Return the argument*/@Overridepublic String joinKeyColumnName(String joinedColumn, String joinedTable) {return columnName( joinedColumn );}/*** Return the property name or propertyTableName*/@Overridepublic String foreignKeyColumnName(String propertyName, String propertyEntityName, String propertyTableName, String referencedColumnName) {String header = propertyName != null ? StringHelper.unqualify( propertyName ) : propertyTableName;if (header == null) throw new AssertionFailure("NamingStrategy not properly filled");return columnName( header ); //+ "_" + referencedColumnName not used for backward compatibility}/*** Return the column name or the unqualified property name*/@Overridepublic 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*/@Overridepublic String logicalCollectionTableName(String tableName,String ownerEntityTable, String associatedEntityTable, String propertyName) {if ( tableName != null ) {return tableName;}else {//use of a stringbuffer to workaround a JDK bugreturn 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*/@Overridepublic String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {return StringHelper.isNotEmpty( columnName ) ?columnName :StringHelper.unqualify( propertyName ) + "_" + referencedColumn;} }

基本上是將ImprovedNamingStrategy類拷貝過來并修改addUnderscores方法的返回值為toUpperCase即可。

(2)將命名策略配置到Hibernate的SessionFactory初始化配置上

由于我們公司是使用spring-boot來進行開發的,所以這里給出spring-boot的配置,是經過驗證的,XML配置尚未經過驗證,配錯了我不背鍋哦。

spring-boot 1.3.8版本配置

spring.jpa.hibernate.naming_strategy: com.xjj.framework.hibernate.cfg.DMNamingStrategy

spring-boot 1.4.0以上版本的配置

spring.jpa.hibernate.naming.physical-strategy=com.xjj.framework.hibernate.cfg.DMNamingStrategy

詳見:http://docs.spring.io/spring-boot/docs/1.4.0.RELEASE/reference/html/howto-data-access.html#howto-configure-jpa-properties

XML方式配置

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="namingStrategy" ref="namingStrategy" /> </bean>

以上就是解決這個問題的方案了。

參考:

1、http://blog.csdn.net/xt0916020331/article/details/49905949

2、https://my.oschina.net/moks/blog/292740

轉載于:https://my.oschina.net/u/1469930/blog/863391

總結

以上是生活随笔為你收集整理的Hibernate修改命名策略的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。