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),但是反过来,不能从身 ...
随机推荐
- 深入理解Tomcat系列之一:系统架构(转)
前言 Tomcat是Apache基金组织下的开源项目,性质是一个Web服务器.下面这种情况很普遍:在eclipse床架一个web项目并部署到Tomcat中,启动tomcat,在浏览器中输入一个类似ht ...
- hdu2639(背包求第k优解)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2639 题意:给出一行价值,一行体积,让你在v体积的范围内找出第k大的值 分析:dp[i][j][k]表 ...
- iOS_UIButton 简单操作
UIButton 风格 typedef NS_ENUM(NSInteger, UIButtonType) { UIButtonTypeCustom = 0, // no button type UIB ...
- Uva10290 - {Sum+=i++} to Reach N
Problem H {sum+=i++} to Reach N Input: standard input Output: standard output Memory Limit: 32 MB A ...
- NGUI研究之在Unity中使用贝塞尔曲线
鼎鼎大名的贝塞尔曲线相信大家都耳熟能详.这两天由于工作的原因须要将贝塞尔曲线加在project中.那么我迅速的研究了一下成果就分享给大家了哦.贝塞尔曲线的原理是由两个点构成的随意角度的曲线,这两个点一 ...
- leetcode -day19 Convert Sorted List to Binary Search Tree
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted ...
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(八)
如无书面授权,请勿转载 第四章,大型项目中Ansible的使用 Roles If your playbooks start expanding beyond what includes can hel ...
- HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧
题意: 每一个' . '有一个姑娘, E是出口,'.'是空地 , 'X' 是墙. 每秒钟每一个姑娘能够走一步(上下左右) 每秒钟每一个出口仅仅能出去一个人 给定n*m的地图, 时限T 问全部姑娘是否能 ...
- 数学思想方法-分布式计算-linux/unix技术基础(5)
shell命令行参数 -bash-4.2$ cat test1.sh#!/bin/shecho "$0 "echo "$1 "echo "$2 ...
- 关于AIX lv 4k offset问题初步了解
关于这个问题我们首先来看一下AIX的vg的3种类型: original vg 普通卷组 big vg 大卷组 scalable vg 动态的或者可扩展的卷组 如何快速区分这三组卷组呢? 通过其参数MA ...