【hibernate】重写物理表名和列明

转载:https://www.cnblogs.com/yangchongxing/p/10357123.html

假设你的数据库命名有这样的需求,表都以 ycx_ 开头,列都以 ycx_ 开头,该如何实现?首先想到的是用 @Entity(name="ycx_user") 或者 @Table(name="tb_user") 重写列明,

用 @Column(name="ycx_username") 重写列明,这样做没有错能够实现,但是表和列特别多时这样会累死人。

使用 org.hibernate.boot.model.naming.PhysicalNamingStrategy 接口统一处理就简单多了。

package cn.ycx.study.hibernate.strategy;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; public class PhysicalNamingStrategyImpl implements PhysicalNamingStrategy {
//重写表名
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
return new Identifier("ycx_" + name.getText(),name.isQuoted());
}
//重写列名
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
return new Identifier("ycx_" +
name.getText(), name.isQuoted());
}
@Override
public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment) {
// TODO Auto-generated method stub
return null;
} @Override
public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) {
// TODO Auto-generated method stub
return null;
} @Override
public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) {
// TODO Auto-generated method stub
return null;
}
}

配置启用命名策略

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
<!-- 设置Hibernate的隔离级别 2:读已提交-->
<property name="hibernate.connection.isolation">2</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 控制台 SQL -->
<property name="hibernate.show_sql">true</property>
<!-- 是否对 SQL 格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- 数据库关键字启用引号 -->
<property name="hibernate.auto_quote_keyword">true</property>
<!-- 添加统一前缀 -->
<property name="hibernate.physical_naming_strategy">cn.ycx.study.hibernate.strategy.PhysicalNamingStrategyImpl</property>

<!--
自动生成数据表的生成策略
create: 每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行。
create-drop: 每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除
update: 第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据 model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等 应用第一次运行起来后才会。
validate: 每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值
-->
<property name="hibernate.hbm2ddl.auto">update</property> <!-- 删除对象后,使其OID置为null
<property name="use_identifier_rollback">true</property>--> <mapping class="cn.ycx.study.hibernate.entity.User"/>
<!-- <mapping class="cn.ycx.study.hibernate.entity.Item"/> -->
</session-factory>
</hibernate-configuration>

自动生成的 SQL 文如下

Hibernate: 

    create table `ycx_User` (
ycx_id bigint not null,
ycx_firstname varchar(255),
ycx_lastname varchar(255),
ycx_username varchar(255),
primary key (ycx_id)
) engine=InnoDB
Hibernate: create table ycx_id_generator (
next_val bigint
)

【hibernate】重写物理表名和列明的更多相关文章

  1. SQL注入——知表名不知列明情景下查询数据

    场景 有某些情况,可以查找到表名,但查找不到列名. MySQL < 5 或 web过滤 information_scema 详解 select `1` from (select 1,2,3,4, ...

  2. Hibernate 中出现表名(XXX) is not mapped 问题

    今天晚上自己试着用Hibernate去搭建一个Web工程,然后去实现一个简单的登录.通过Hibernate?做查询操作的时候总是报出这样的错:users is?not?mapped. 于是乎去检查了下 ...

  3. sqlserver 各种判断是否存在(表名、函数、存储过程等)

    库是否存在 if exists(select * from master..sysdatabases where name=N'库名') print 'exists'elseprint 'not ex ...

  4. sqlserver 常见的表名修改

    查看表:exec sp_help 表名 查看列: exec sp_columns 表名 查看列:select * from information_schema.columns where table ...

  5. MySQL 查询 数据库有多少表 表名是哪些

    1.查询sjcenter数据库里开头为sj_demo和sj_onlyinv的所有表的总条数 select sum(table_rows) from (select table_name,table_r ...

  6. SQL Server 2008R2 Set IDENTITY_INSERT 表名 ON/OFF不能与insert into select 的语句一起执行?

    大家都知数据库表中的列可以自增长,但是有时候我们需要插入数据的时候会指定这一列的数据. 这时候我们可以很简单的利用sql语句来执行新增一条的数据,如下: set IDENTITY_INSER 表名 o ...

  7. hibernate 获取实体的表名、主键名、列名(转载+修改)

    package com.escs.utils; import java.util.Iterator; import org.hibernate.cfg.AnnotationConfiguration; ...

  8. SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...

  9. SQLSERVER和ORACLE系统表获取表名 列名以及列的注释

    在工作中从数据库取的数据要导出来,但是发现导出的EXCEL中列名都是字段名(英文),为此搜集资料怎么把字段名变为中文名称,而发现ORACLE和SQLSERVER(用的SQLSERVER2008R2)又 ...

随机推荐

  1. 【编程题与分析题】Javascript 之继承的多种实现方式和优缺点总结

    [!NOTE] 能熟练掌握每种继承方式的手写实现,并知道该继承实现方式的优缺点. 原型链继承 function Parent() { this.name = 'zhangsan'; this.chil ...

  2. 领扣(LeetCode)两数之和II - 输入有序数组 个人题解

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...

  3. django_5:表单1——文件上传

    上传文件1 class UserForm(forms.Form): name = forms.CharField() headImg = forms.FileField() def regist(re ...

  4. 【dp】you are the one

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4283 题解: 当最优解下, a1在j的位置排出, 则a2 ——aj-1 和 aj——an为两个独立事件 ...

  5. [ubuntu篇] 使用Hexo建立个人博客,自定义域名https加密,搜索引擎google,baidu,360收录

    为了更好的阅读体验,欢迎阅读原文.原文链接在此. Part 1: Using Github Pages and Hexo to manage personal blogs. Series Part 1 ...

  6. 在WebView中加载HTML页面时显示进度对话框的方法

    webView.setWebViewClient(new WebViewClient(){            ProgressDialog prDialog;            @Overri ...

  7. Excel 如何做不定长区间汇总统计

    第一步:创建数据-区间 辅助表(注意:首列值必须以升序排列,为后面vlookup模糊匹配做准备) 第二步:用vlookup模糊匹配生成一个新的“金额区间”字段 第三步:以“金额区间”字段为行透视汇总

  8. MySql分库分表与分区的区别和思考

    一.分分合合 说过很多次,不要拘泥于某一个技术的一点,技术是相通的.重要的是编程思想,思想是最重要的.当数据量大的时候,需要具有分的思想去细化粒度.当数据量太碎片的时候,需要具有合的思想来粗化粒度. ...

  9. 原生JS通过类名(className)获取dom元素

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. C#取视频某一帧图片

    首先下载 ffmpeg http://ffmpeg.org/ 注意一定要从官网下载,其他地方可以会有问题 解压后在 bin 目录下找到 ffmpeg.exe 用到的使命是 -i 视频地址 -ss 第几 ...