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]的更多相关文章

  1. 02.Hibernate映射基础

    前言:Hibernate的核心功能是根据数据库到实体类的映射,自动从数据库绑定数据到实体类.使我们操作实体类(Java对象)就能对数据库进行增.删.查.改,而不用调用JDBC API使数据操作变得简单 ...

  2. hibernate映射文件

    Hibernate的持久化类和关系数据库之间的映射通常是用一个XML文档来定义的.该文档通过一系列XML元素的配置,来将持久化类与数据库表之间建立起一一映射.这意味着映射文档是按照持久化类的定义来创建 ...

  3. hibernate映射的 关联关系:有 一对多关联关系,一对一关联关系,多对多关联关系,继承关系

    hibernate环境配置:导包.... 单向n-1:单向 n-1 关联只需从 n 的一端可以访问 1 的一端 <many-to-one> 元素来映射组成关系: name: 设定待映射的持 ...

  4. Hibernate 映射关系

    映射组成关系 •建立域模型和关系数据模型有着不同的出发点: –域模型: 由程序代码组成, 通过细化持久化类的的粒度可提高代码的可重用性, 简化编程 –在没有数据冗余的情况下, 应该尽可能减少表的数目, ...

  5. Hibernate映射类型对照表

    Hibernate映射类型对照表 java类型  Hibernate映射类型  SQL类型 java.math.BigDecimal big_decimal numeric byte[] binary ...

  6. hibernate Java 时间和日期类型的 Hibernate 映射

    基础知识: 在 Java 中, 代表时间和日期的类型包含: java.util.Date 和 java.util.Calendar. 此外, 在 JDBC API 中还提供了 3 个扩展了 java. ...

  7. [转]Hibernate映射的基本操作

       ++YONG原创,转载请注明http://blog.csdn.net/qjyong/article/details/1829672          Hibernate映射主要是通过对象关系映射 ...

  8. 【SSH系列】Hibernate映射 -- 一对多关联映射

        映射原理       一对多关联映射和多对一关联映射的映射原理是一样一样的,所以说嘛,知识都是相通的,一通百通,为什么说一对多关联映射和多对一关联映射是一样的呢?因为她们都是在多的一端加入一个 ...

  9. 【SSH系列】hibernate映射 -- 一对一双向关联映射

    开篇前言 上篇博文[SSH进阶之路]hibernate映射--一对一单向关联映射,小编介绍了一对一的单向关联映射,单向是指只能从人(Person)这端加载身份证端(IdCard),但是反过来,不能从身 ...

随机推荐

  1. 使用 JQueryMobile 点击超链接提示“error loading page” 错误

    使用jquery mobile创建dialog时出现加载错误,“Error Loading Page”. 原因是:jquery mobile页面默认采用ajax方式进行交互,而ajax方式下是不支持f ...

  2. 日期格式化标签<fmt:formatDate>&<fmt:setTimeZone>时区标签的使用demo

    日期格式化标签<fmt:formatDate>&<fmt:setTimeZone>时区标签的使用demo <%@ page contentType="t ...

  3. umlの实现图

    在uml中大部分模型描写叙述了逻辑和设计方面的信息: 用例图知道期望 类图能够知道问题域的词汇(类.对象) 状态图.交互图和活动图能够知道类图中的词汇是怎样写作完毕行为的(逻辑结构) 实现图是用来描写 ...

  4. cocos2d_x_05_Box2D物理引擎

    一.认识Box2D 帮助文档,共69页 二.创建一个物理世界 先导入主头文件 #include <Box2D/Box2D.h> 三.物理世界一览 像素转成米 的比例因子 就是32 三.运动 ...

  5. 孙鑫HTML视频学习总结

    1.   HTML中元素和标签 元素是由单个或一对标签定义的包含范围.一个标签就是左右分别有一个小于号(<)和大于号(>)的字符串.开始标签是指以不以斜杠(/)开头的标签,其内是一串允许的 ...

  6. 【译】ASP.NET MVC 5 教程 - 7:Edit方法和Edit视图详解

    原文:[译]ASP.NET MVC 5 教程 - 7:Edit方法和Edit视图详解 在本节中,我们继续研究生成的Edit方法和视图.但在研究之前,我们先将 release date 弄得好看一点.打 ...

  7. poj1155(树形dp)

    题目链接:http://poj.org/problem?id=1155 题意:电视台要直播一场比赛,电视网络刚好形成了一棵树,其中有M个为客户端,其他的为中转站,其中中转站与中转站以及中转站与客户端之 ...

  8. extjs4 分页工具栏pagingtoolbar的每页显示数据combobox下拉框

    var itemsPerPage = 20; var combo; //创建数据源store Ext.define('recordStore', { extend : 'Ext.data.Store' ...

  9. VMware WorkStation安装时提示The MSI failed

    以前安装过其他版本的VMware workstation卸载不完全造成的 先把所有VMware相关服务关闭,然后打开注册表,搜索所有VMware相关键值,删除掉,然后再安装就可以了 前提是你机器上没有 ...

  10. jps查看java进程中哪个线程在消耗系统资源

    jps或ps -ef|grep java可以看到有哪些java进程,这个不用说了.但值得一提的是jps命令是依赖于/tmp下的某些文件 的. 而某些操作系统,定期会清理掉/tmp下的文件,导致jps无 ...