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. tolua 有些功能可以用(经过测试)

    tolua 提供几个 C++ 与 Lua 进行数据交换的工具函数. ~~ tolua.type 返回一个 C++ 对象的类型描写叙述字符串. local node = display.newNode( ...

  2. I2C分析三

    1 引言 IIC (Inter-Integrated Circuit1总线是一种由Philips公司开发的2线式串行总线,用于连接微控制器及其外围设备.它是同步通信的一种特殊形式,具有接口线少.控制方 ...

  3. Java中定时器的使用

    import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.T ...

  4. 如何通过shell脚本操作MongoDB

    通过shell脚本对MongoDB进行自动化操作 运行写好的 ./show.sh 脚本 发现能够建立mongo链接 #!/bin/sh mongo WordPress --eval "sho ...

  5. mv目录前后要加斜杠,否则会当成文件

    mv目录要加斜杠,否则会当成文件

  6. Linux系统部署规范v1.0

    Linux系统部署规范v1.0 目的: 1.尽可能减少线上操作: 2.尽可能实现自动化部署: 3.尽可能减少安装服务和启动的服务: 4.尽可能使用安全协议提供服务: 5.尽可能让业务系统单一: 6.尽 ...

  7. Flipping Game(枚举)

    Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. HttpURLConnection请求数据流的写入(write)和读取(read)

    URLConnection类给应用 程序 和web资源之间架设起了通信的桥梁,这些web资源通常是通过url来标记的,本文将讲述如何使用HttpURLConnection来访问web页面(发送数据流) ...

  9. Sql SUBSTR函数

    SUBSTR( ) 从一个字符表达式或备注字段中返回一个字符串.该字符串起始于字符表达式或备注字段的指定位置,到指定数目字符结束. 例如: SUBSTR(“A1”, 1 1]) 结果:A

  10. ecshop购物流程中去掉email邮箱

    首先打开includes\lib_order.php,在第1688行左右找到并删除 !empty($consignee['email']) && 接着打开js\shopping_flo ...