【hibernate】重写物理表名和列明
【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】重写物理表名和列明的更多相关文章
- SQL注入——知表名不知列明情景下查询数据
场景 有某些情况,可以查找到表名,但查找不到列名. MySQL < 5 或 web过滤 information_scema 详解 select `1` from (select 1,2,3,4, ...
- Hibernate 中出现表名(XXX) is not mapped 问题
今天晚上自己试着用Hibernate去搭建一个Web工程,然后去实现一个简单的登录.通过Hibernate?做查询操作的时候总是报出这样的错:users is?not?mapped. 于是乎去检查了下 ...
- sqlserver 各种判断是否存在(表名、函数、存储过程等)
库是否存在 if exists(select * from master..sysdatabases where name=N'库名') print 'exists'elseprint 'not ex ...
- sqlserver 常见的表名修改
查看表:exec sp_help 表名 查看列: exec sp_columns 表名 查看列:select * from information_schema.columns where table ...
- MySQL 查询 数据库有多少表 表名是哪些
1.查询sjcenter数据库里开头为sj_demo和sj_onlyinv的所有表的总条数 select sum(table_rows) from (select table_name,table_r ...
- SQL Server 2008R2 Set IDENTITY_INSERT 表名 ON/OFF不能与insert into select 的语句一起执行?
大家都知数据库表中的列可以自增长,但是有时候我们需要插入数据的时候会指定这一列的数据. 这时候我们可以很简单的利用sql语句来执行新增一条的数据,如下: set IDENTITY_INSER 表名 o ...
- hibernate 获取实体的表名、主键名、列名(转载+修改)
package com.escs.utils; import java.util.Iterator; import org.hibernate.cfg.AnnotationConfiguration; ...
- SQL Server 动态行转列(参数化表名、分组列、行转列字段、字段值)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现代码(SQL Codes) 方法一:使用拼接SQL,静态列字段: 方法二:使用拼接SQL, ...
- SQLSERVER和ORACLE系统表获取表名 列名以及列的注释
在工作中从数据库取的数据要导出来,但是发现导出的EXCEL中列名都是字段名(英文),为此搜集资料怎么把字段名变为中文名称,而发现ORACLE和SQLSERVER(用的SQLSERVER2008R2)又 ...
随机推荐
- nyoj 844-A+B Problem(V) (string[::-1] 字符串反转)
844-A+B Problem(V) 内存限制:64MB 时间限制:1000ms 特判: No 通过数:14 提交数:17 难度:1 题目描述: 做了A+B Problem之后,Yougth感觉太简单 ...
- 防火墙和SELinux
在/etc/sysconfig/selinux中修改SELINUX=disabled关闭SELinux 执行systemctl disable firewalld关闭防火墙 然后重启计算机
- 解放双手,在PC端进行Android真机调试
scrcpy简介(拼写是scrcpy,非Python爬虫框架Scrapy) 简单地来说,scrcpy就是通过adb调试的方式来将手机屏幕投到电脑上,并可以通过电脑控制您的Android设备.它可以通过 ...
- vue项目中安装使用vux
vux是个vue的移动端框架. 目前移动端UI框架这么多,为啥选择vux呢?vux虽然说是个个人维护项目,但是有15000+个star,应该不比其他的团队开源框架差. 最重要的是,目前要做微信公众号和 ...
- startup启动不起来关于监听的问题
问题描述:要在sqlplus中启动到startup状态,但是提示我没有监听,本来以为启动一下就可以,但是connecting to一直卡半天,stop都停止不了 1.发现监听有问题,前去更改 SQL& ...
- python3 之 文件read方法(read、readline、readlines)
目录 一.read方法 二.readline方法 三.readlines方法 正文 python3中,读取文件有三种方法:read().readline().readlines(). 此三种方法,均支 ...
- C#Windows Forms 使MessageBox顶层显示--xdd
方法1. MessageBox.Show("Text", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Inf ...
- day 36 初始前端 html语言
参考博客https://www.cnblogs.com/majj/p/9056951.html进行学习 html标签 特征: .空白折叠现象 .对空格和换行不敏感 .标签要严格封闭 p标签的嵌套 多注 ...
- MovibleNet
MobileNet MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications MobileN ...
- scikit-learn网格搜索来进行高效的参数调优
内容概要¶ 如何使用K折交叉验证来搜索最优调节参数 如何让搜索参数的流程更加高效 如何一次性的搜索多个调节参数 在进行真正的预测之前,如何对调节参数进行处理 如何削减该过程的计算代价 1. K折交叉验 ...