【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
    ) 
原文地址:https://www.cnblogs.com/yangchongxing/p/10357123.html