Hibernate 映射字段问题[ImprovedNamingStrategy]
Hibernate 使用JPA 对于映射有3种规则能够配置:DefaultNamingStrategy,ImprovedNamingStrategy,EJB3NamingStrategy
这里仅仅说ImprovedNamingStrategy,其它自行看Hibernate代码,ImprovedNamingStrategy的代码例如以下,是一个singleton instance:
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.cfg; import java.io.Serializable; import org.hibernate.util.StringHelper;
import org.hibernate.AssertionFailure; /**
* An improved naming strategy that prefers embedded
* underscores to mixed case names
* @see DefaultNamingStrategy the default strategy
* @author Gavin King
*/
public class ImprovedNamingStrategy implements NamingStrategy, Serializable { /**
* A convenient singleton instance
*/
public static final NamingStrategy INSTANCE = new ImprovedNamingStrategy(); /**
* Return the unqualified class name, mixed case converted to
* underscores
*/
public String classToTableName(String className) {
return addUnderscores( StringHelper.unqualify(className) );
}
/**
* Return the full property path with underscore seperators, mixed
* case converted to underscores
*/
public String propertyToColumnName(String propertyName) {
return addUnderscores( StringHelper.unqualify(propertyName) );
}
/**
* Convert mixed case to underscores
*/
public String tableName(String tableName) {
return addUnderscores(tableName);
}
/**
* Convert mixed case to underscores
*/
public String columnName(String columnName) {
return addUnderscores(columnName);
} protected static String addUnderscores(String name) {
StringBuffer buf = new StringBuffer( 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().toLowerCase();
} public String collectionTableName(
String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable,
String propertyName
) {
return tableName( ownerEntityTable + '_' + propertyToColumnName(propertyName) );
} /**
* Return the argument
*/
public String joinKeyColumnName(String joinedColumn, String joinedTable) {
return columnName( joinedColumn );
} /**
* Return the property name or propertyTableName
*/
public 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
*/
public 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
*/
public String logicalCollectionTableName(String tableName,
String ownerEntityTable, String associatedEntityTable, String propertyName
) {
if ( tableName != null ) {
return tableName;
}
else {
//use of a stringbuffer to workaround a JDK bug
return 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
*/
public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
return StringHelper.isNotEmpty( columnName ) ?
columnName :
StringHelper.unqualify( propertyName ) + "_" + referencedColumn;
}
}
addUnderscores 用于处理 当表名和列名在Java的种规则符合 UserNameTable(表)和 userNameColumn(列),就会被解析为user_name_table 和 user_name_column ,详细return的处理的是propertyToColumnName。 but呢,假设一旦配置了这个规则,spring +jpa配置例如以下:
-
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
就会忽略了凝视中的@Column中的name,事实上这个name地方就是为了映射数据库字段,结果配置了这个就不care了。我去。看下hibernate解释
https://hibernate.atlassian.net/browse/HHH-8616
认为是这是一个bug,不知道为啥就这样fix bug 了。
如今有7-8张表的规则没有符合这个怎么办呢, 重写了例如以下。
/*
* Copyright 2012-2014 sencloud.com.cn. All rights reserved.
* Support: http://www.sencloud.com.cn
* License: http://www.sencloud.com.cn/license
*/
package com.sencloud.hibernate.cfg; import java.io.Serializable; import org.hibernate.cfg.ImprovedNamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.util.StringHelper; /**
* ImprovedNamingStrategy- hibernate映射规则
*
* @author xutianlong
* @version 3.0
*/
public class MoShopImprovedNamingStrategy extends ImprovedNamingStrategy
implements NamingStrategy, Serializable {
/**
*
*/
private static final long serialVersionUID = 3088474161734101900L; public String propertyToColumnName(String propertyName) {
if (propertyName.endsWith("__")) {
return propertyName.replace("__", "").toLowerCase();
}
return addUnderscores(StringHelper.unqualify(propertyName));
}
}
改下配置:
<prop key="hibernate.ejb.naming_strategy">com.sencloud.hibernate.cfg.MoShopImprovedNamingStrategy</prop>
那么怎么使用这个呢? propertyName必须以 "__"结尾了,然后在规则中统一toLowerCase。 有点那个啥了。
@Column(nullable = false, length=100)
private String userName__;
Hibernate 映射字段问题[ImprovedNamingStrategy]的更多相关文章
- 02.Hibernate映射基础
前言:Hibernate的核心功能是根据数据库到实体类的映射,自动从数据库绑定数据到实体类.使我们操作实体类(Java对象)就能对数据库进行增.删.查.改,而不用调用JDBC API使数据操作变得简单 ...
- hibernate映射文件
Hibernate的持久化类和关系数据库之间的映射通常是用一个XML文档来定义的.该文档通过一系列XML元素的配置,来将持久化类与数据库表之间建立起一一映射.这意味着映射文档是按照持久化类的定义来创建 ...
- hibernate映射的 关联关系:有 一对多关联关系,一对一关联关系,多对多关联关系,继承关系
hibernate环境配置:导包.... 单向n-1:单向 n-1 关联只需从 n 的一端可以访问 1 的一端 <many-to-one> 元素来映射组成关系: name: 设定待映射的持 ...
- Hibernate 映射关系
映射组成关系 •建立域模型和关系数据模型有着不同的出发点: –域模型: 由程序代码组成, 通过细化持久化类的的粒度可提高代码的可重用性, 简化编程 –在没有数据冗余的情况下, 应该尽可能减少表的数目, ...
- Hibernate映射类型对照表
Hibernate映射类型对照表 java类型 Hibernate映射类型 SQL类型 java.math.BigDecimal big_decimal numeric byte[] binary ...
- hibernate Java 时间和日期类型的 Hibernate 映射
基础知识: 在 Java 中, 代表时间和日期的类型包含: java.util.Date 和 java.util.Calendar. 此外, 在 JDBC API 中还提供了 3 个扩展了 java. ...
- [转]Hibernate映射的基本操作
++YONG原创,转载请注明http://blog.csdn.net/qjyong/article/details/1829672 Hibernate映射主要是通过对象关系映射 ...
- 【SSH系列】Hibernate映射 -- 一对多关联映射
映射原理 一对多关联映射和多对一关联映射的映射原理是一样一样的,所以说嘛,知识都是相通的,一通百通,为什么说一对多关联映射和多对一关联映射是一样的呢?因为她们都是在多的一端加入一个 ...
- 【SSH系列】hibernate映射 -- 一对一双向关联映射
开篇前言 上篇博文[SSH进阶之路]hibernate映射--一对一单向关联映射,小编介绍了一对一的单向关联映射,单向是指只能从人(Person)这端加载身份证端(IdCard),但是反过来,不能从身 ...
随机推荐
- hdu2389(HK算法)
传送门:Rain on your Parade 题意:t个单位时间后开始下雨,给你N个访客的位置(一维坐标平面内)和他的移动速度,再给M个雨伞的位置,问在下雨前最多有多少人能够拿到雨伞(两个人不共用一 ...
- Graphical Shell with WebShell - WebOS Internals
Graphical Shell with WebShell - WebOS Internals Graphical Shell with WebShell From WebOS Internals J ...
- c++程序猿经典面试题
1.请问i的值会输出什么? #include"iostream.h" int i=1; void main() { int i=i; cout<<i<<en ...
- 在CodeBlocks 开发环境中配置使用OpenCV (ubuntu系统)
CodeBlocks是一个开放源代码的全功能的跨平台C/C++集成开发环境.CodeBlocks由纯粹的C++语言开发完毕,它使用了蓍名的图形界面库wxWidgets.对于追求完美的C++程序猿,再也 ...
- B. 沙漠之旅(分组背包)
B. 沙漠之旅 Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: % ...
- Wix学习整理(7)——在开始菜单中为HelloWorld添加卸载快捷方式
原文:Wix学习整理(7)--在开始菜单中为HelloWorld添加卸载快捷方式 通过前面的几篇随笔,我们已经给我们的HelloWorld提供了填写注册表信息,以及开始菜单快捷方式和桌面快捷方式.这些 ...
- 多个UpdatePanel控件相互引发刷新的使用
原文:多个UpdatePanel控件相互引发刷新的使用 ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果.其中的UpdatePanel就是设置页面中异 步局 ...
- Redis在win7上的安装与可视化应用
Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...
- httpcomponents-client-ga(4.5)
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/ Chapter 1. Fundamentals Prev Next ...
- OCP-1Z0-051-题目解析-第30题
30. Evaluate the following CREATE TABLE commands: CREATE TABLE orders (ord_no NUMBER(2) CONSTRAINT o ...