最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Hibernate命名策略詳解

 更新時間:2017年01月24日 09:39:34   作者:突破渴望  
本文主要介紹了Hibernate命名策略。具有很好的參考價值,下面跟著小編一起來看下吧

hibernate的命名策略,可以減少對數(shù)據(jù)庫標(biāo)識符命名的維護,進一步減少這部份命名的重復(fù)性代碼量,以提高維護。

hibernate的命名方式,有兩類,一類是顯式命名,一類是隱式命名。

1)顯式命名:在映射配置時,設(shè)置的數(shù)據(jù)庫表名,列名等,就是進行顯式命名。

2)隱式命名:顯式命名一般不是必要的,所以可以選擇當(dāng)不設(shè)置名稱,這時就交由hibernate進行隱式命名,另外隱式命名還包括那些不能進行顯式命名的數(shù)據(jù)庫標(biāo)識符。接口ImplicitNamingStrategy,就是用于實現(xiàn)隱式命名。

3)過濾命名:接口PhysicalNamingStrategy,用于對顯式命名或隱式命名進一步過濾處理。

示例:

TestTable1Impl.java

@Entity
// 隱式命名表名
@Table
public class TestTable1Impl {
  //---------------------------------------------------------------
  // Field
  //---------------------------------------------------------------
  @Id
  @Column()
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long testId;
  @Column(length = 20)
  private String testName;
  @ManyToOne
  private TestTable2Impl testForeign;
  //---------------------------------------------------------------
  // Method
  //---------------------------------------------------------------
  public Long getId() {
    return testId;
  }
  public void setId(Long id) {
    this.testId = id;
  }
  public String getName(){
    return testName;
  }
  public void setName(String name){
    this.testName = name;
  }
  public TestTable2Impl getTestForeign() {
    return testForeign;
  }
  public void setTestForeign(TestTable2Impl testForeign) {
    this.testForeign = testForeign;
  }
}

TestTable2Impl.java

@Entity
// 顯式命名表名
@Table(name = "TestTable2Impl")
public class TestTable2Impl {
  //---------------------------------------------------------------
  // Field
  //---------------------------------------------------------------
  @Id
  @Column()
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long testId;
  @Column(length = 20)
  private String testName;
  //---------------------------------------------------------------
  // Method
  //---------------------------------------------------------------
  public Long getId() {
    return testId;
  }
  public void setId(Long id) {
    this.testId = id;
  }
  public String getName(){
    return testName;
  }
  public void setName(String name){
    this.testName = name;
  }
}

MyImplicitNamingStrategyImpl.java

public class MyImplicitNamingStrategyImpl extends ImplicitNamingStrategyJpaCompliantImpl implements ImplicitNamingStrategy {
  @Override
  public Identifier determinePrimaryTableName(ImplicitEntityNameSource source) {
    Identifier name = super.determinePrimaryTableName(source);
    Identifier result = toStandard(name, "Impl");
    System.out.println("ImplicitNamingStrategy / PrimaryTableName -> \n\t" + name + " => " + result);
    return result;
  }
  private Identifier toStandard(Identifier name, String... removeSuffixes){
    if(removeSuffixes == null)
      return name;
    if(name == null)
      return null;
    String text = name.getText();
    if(removeSuffixes != null){
      for(String suffix : removeSuffixes){
        if(text.endsWith(suffix))
          text = text.substring(0, text.length() - suffix.length());
      }
    }
    return new Identifier(text, name.isQuoted());
  }
  @Override
  public Identifier determineJoinTableName(ImplicitJoinTableNameSource source) {
    Identifier name = super.determineJoinTableName(source);
    System.out.println("ImplicitNamingStrategy / JoinTableName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineCollectionTableName(ImplicitCollectionTableNameSource source) {
    Identifier name = super.determineCollectionTableName(source);
    System.out.println("ImplicitNamingStrategy / CollectionTableName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineDiscriminatorColumnName(ImplicitDiscriminatorColumnNameSource source) {
    Identifier name = super.determineDiscriminatorColumnName(source);
    System.out.println("ImplicitNamingStrategy / DiscriminatorColumnName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineTenantIdColumnName(ImplicitTenantIdColumnNameSource source) {
    Identifier name = super.determineTenantIdColumnName(source);
    System.out.println("ImplicitNamingStrategy / TenantIdColumnName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineIdentifierColumnName(ImplicitIdentifierColumnNameSource source) {
    Identifier name = super.determineIdentifierColumnName(source);
    System.out.println("ImplicitNamingStrategy / IdentifierColumnName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineBasicColumnName(ImplicitBasicColumnNameSource source) {
    Identifier name = super.determineBasicColumnName(source);
    System.out.println("ImplicitNamingStrategy / BasicColumnName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineJoinColumnName(ImplicitJoinColumnNameSource source) {
    Identifier name = super.determineJoinColumnName(source);
    final String result;
    if ( source.getNature() == ImplicitJoinColumnNameSource.Nature.ELEMENT_COLLECTION || source.getAttributePath() == null ) {
      result = transformEntityName( source.getEntityNaming() );
    } else {
      result = transformAttributePath( source.getAttributePath() );
    }
    System.out.println("ImplicitNamingStrategy / JoinColumnName -> \n\t" + name + " => " + result);
    return toIdentifier( result, source.getBuildingContext() );
  }
  @Override
  public Identifier determinePrimaryKeyJoinColumnName(ImplicitPrimaryKeyJoinColumnNameSource source) {
    Identifier name = super.determinePrimaryKeyJoinColumnName(source);
    System.out.println("ImplicitNamingStrategy / PrimaryKeyJoinColumnName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineAnyDiscriminatorColumnName(ImplicitAnyDiscriminatorColumnNameSource source) {
    Identifier name = super.determineAnyDiscriminatorColumnName(source);
    System.out.println("ImplicitNamingStrategy / AnyDiscriminatorColumnName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineAnyKeyColumnName(ImplicitAnyKeyColumnNameSource source) {
    Identifier name = super.determineAnyKeyColumnName(source);
    System.out.println("ImplicitNamingStrategy / AnyKeyColumnName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineMapKeyColumnName(ImplicitMapKeyColumnNameSource source) {
    Identifier name = super.determineMapKeyColumnName(source);
    System.out.println("ImplicitNamingStrategy / MapKeyColumnName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineListIndexColumnName(ImplicitIndexColumnNameSource source) {
    Identifier name = super.determineListIndexColumnName(source);
    System.out.println("ImplicitNamingStrategy / ListIndexColumnName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineForeignKeyName(ImplicitForeignKeyNameSource source) {
    Identifier name = super.determineForeignKeyName(source);
    String result = null;
    String tableName = source.getTableName().getText();
    if(tableName.startsWith(TableNamingConfig.TABLE_PREFIX))
      tableName = tableName.substring(TableNamingConfig.TABLE_PREFIX.length());
    if(source.getColumnNames().size() == 1){
      result = TableNamingConfig.FOREIGN_KEY_PREFIX + tableName + "_" + source.getColumnNames().get(0).getText();
    } else {
      String columnName = source.getReferencedTableName().getText();
      if(columnName.startsWith(TableNamingConfig.TABLE_PREFIX))
        columnName = columnName.substring(TableNamingConfig.TABLE_PREFIX.length());
      result = TableNamingConfig.FOREIGN_KEY_PREFIX + tableName + "_" + columnName;
    }
    System.out.println("ImplicitNamingStrategy / ForeignKeyName -> \n\t" + name + " => " + result);
    return new Identifier(result, name.isQuoted());
  }
  @Override
  public Identifier determineUniqueKeyName(ImplicitUniqueKeyNameSource source) {
    Identifier name = super.determineUniqueKeyName(source);
    System.out.println("ImplicitNamingStrategy / UniqueKeyName -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier determineIndexName(ImplicitIndexNameSource source) {
    Identifier name = super.determineIndexName(source);
    System.out.println("ImplicitNamingStrategy / IndexName -> \n\t" + name);
    return name;
  }
}

MyPhysicalNamingStrategyImpl.java

public class MyPhysicalNamingStrategyImpl implements PhysicalNamingStrategy {
  @Override
  public Identifier toPhysicalCatalogName(Identifier name, JdbcEnvironment jdbcEnvironment) {
    System.out.println("PhysicalNamingStrategy / catalog -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier toPhysicalSchemaName(Identifier name, JdbcEnvironment jdbcEnvironment) {
    System.out.println("PhysicalNamingStrategy / schema -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment jdbcEnvironment) {
    Identifier result = toStandard(name, "tb_");
    System.out.println("PhysicalNamingStrategy / table -> \n\t" + name + " => " + result);
    return result;
  }
  @Override
  public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment jdbcEnvironment) {
    System.out.println("PhysicalNamingStrategy / sequence -> \n\t" + name);
    return name;
  }
  @Override
  public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
    Identifier result = toStandard(name);
    System.out.println("PhysicalNamingStrategy / column -> \n\t" + name + " => " + result);
    return result;
  }
  private Identifier toStandard(Identifier name){
    return toStandard(name, null);
  }
  private Identifier toStandard(Identifier name, String prefix){
    if(name == null)
      return null;
    String text = name.getText();
    StringBuffer buffer = new StringBuffer();
    if(prefix != null)
      buffer.append(prefix);
    char[] chars = text.toCharArray();
    for(int i=0, len=chars.length; i<len; i++){
      char c1 = chars[i];
      if(c1 >= 'A' && c1 <= 'Z'){
        if(i > 0 && i + 1 < len){
          if(chars[i + 1] < 'A' || chars[i + 1] > 'Z')
            buffer.append('_');
        }
        c1 = (char) (c1 - 'A' + 'a');
      }
      buffer.append(c1);
    }
    return new Identifier(buffer.toString(), name.isQuoted());
  }
}

TableNamingConfig.java

public class TableNamingConfig {
  public static final String TABLE_PREFIX = "tb_";
  public static final String FOREIGN_KEY_PREFIX = "fk_";
}

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-4.1.xsd">
  <!-- 配置數(shù)據(jù)源 -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useSSL=false"></property>
    <property name="user" value="root"></property>
    <property name="password" value="123456"></property>
  </bean>
  <bean id="physicalNamingStrategy" class="test.MyPhysicalNamingStrategyImpl"></bean>
  <bean id="implicitNamingStrategy" class="test.MyImplicitNamingStrategyImpl"></bean>
  <bean id="sessionFactory"    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
      <list>
        <!-- 可以加多個包 -->
        <value>test</value>
      </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.format_sql">true</prop>
        <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
      </props>
    </property>
    <property name="physicalNamingStrategy" ref="physicalNamingStrategy"></property>
    <property name="implicitNamingStrategy" ref="implicitNamingStrategy"></property>
  </bean>
</beans>

Test.java

public class Test {
  public static void main(String[] params){
    // 命名策略
    new Test().test();
    /*
      PhysicalNamingStrategy / catalog -> 
        null
      PhysicalNamingStrategy / catalog -> 
        null
      PhysicalNamingStrategy / column -> 
        DTYPE => dtype
      ImplicitNamingStrategy / PrimaryTableName -> 
        TestTable1Impl => TestTable1
      PhysicalNamingStrategy / table -> 
        TestTable1 => tb_test_table1
      ImplicitNamingStrategy / BasicColumnName -> 
        testId
      PhysicalNamingStrategy / column -> 
        testId => test_id
      ImplicitNamingStrategy / BasicColumnName -> 
        testId
      ImplicitNamingStrategy / BasicColumnName -> 
        testForeign
      PhysicalNamingStrategy / column -> 
        testForeign => test_foreign
      ImplicitNamingStrategy / BasicColumnName -> 
        testName
      PhysicalNamingStrategy / column -> 
        testName => test_name
      ImplicitNamingStrategy / BasicColumnName -> 
        testName
      PhysicalNamingStrategy / column -> 
        DTYPE => dtype
      PhysicalNamingStrategy / table -> 
        TestTable2Impl => tb_test_table2_impl
      ImplicitNamingStrategy / BasicColumnName -> 
        testId
      PhysicalNamingStrategy / column -> 
        testId => test_id
      ImplicitNamingStrategy / BasicColumnName -> 
        testId
      ImplicitNamingStrategy / BasicColumnName -> 
        testName
      PhysicalNamingStrategy / column -> 
        testName => test_name
      ImplicitNamingStrategy / BasicColumnName -> 
        testName
      ImplicitNamingStrategy / JoinColumnName -> 
        testForeign_testId => testForeign
      PhysicalNamingStrategy / column -> 
        testForeign => test_foreign
      ImplicitNamingStrategy / ForeignKeyName -> 
        FKlnurug7wfle1u6fc5oulnrx94 => fk_test_table1_test_foreign
      Hibernate: 
        alter table tb_test_table1 
          drop 
          foreign key fk_test_table1_test_foreign
      Hibernate: 
        drop table if exists tb_test_table1
      Hibernate: 
        drop table if exists tb_test_table2_impl
      Hibernate: 
        create table tb_test_table1 (
          test_id bigint not null auto_increment,
          test_name varchar(20),
          test_foreign bigint,
          primary key (test_id)
        )
      Hibernate: 
        create table tb_test_table2_impl (
          test_id bigint not null auto_increment,
          test_name varchar(20),
          primary key (test_id)
        )
      Hibernate: 
        alter table tb_test_table1 
          add constraint fk_test_table1_test_foreign 
          foreign key (test_foreign) 
          references tb_test_table2_impl (test_id)
      Hibernate: 
        alter table tb_test_table1 
          drop 
          foreign key fk_test_table1_test_foreign
      Hibernate: 
        drop table if exists tb_test_table1
      Hibernate: 
        drop table if exists tb_test_table2_impl
     */
  }
  public void test(){
    ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml", this.getClass());
    SessionFactory factory = null;
    try {
      factory = (SessionFactory) context.getBean("sessionFactory");
    } finally {
      if(factory != null){
        factory.close();
        factory = null;
      }
    }
  }
}

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

  • SpringBoot快速接入DeepSeek?api(帶頁面)保姆級教程

    SpringBoot快速接入DeepSeek?api(帶頁面)保姆級教程

    這篇文章主要介紹了如何在Java端接入DeepSeek?API,包括申請APIkey、項目結(jié)構(gòu)展示、編寫controller和前端界面、以及測試啟動項目的過程,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-03-03
  • JavaGUI使用標(biāo)簽與按鈕方法詳解

    JavaGUI使用標(biāo)簽與按鈕方法詳解

    這篇文章主要介紹了JavaGUI使用標(biāo)簽與按鈕方法,前段時間學(xué)了GUI,總體上概念還是有點模糊,于是決定花點時間簡單整理下。先簡單介紹一下GUI,GUI就是圖形用戶界面
    2023-03-03
  • Java項目中classpath類路徑是什么

    Java項目中classpath類路徑是什么

    classpath指的是類路徑,也就是編譯之后的target文件夾下的WEB-INF/class文件夾,下面這篇文章主要給大家介紹了關(guān)于Java項目中classpath類路徑是什么的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • 比較排序之快速排序(實例代碼)

    比較排序之快速排序(實例代碼)

    下面小編就為大家?guī)硪黄容^排序之快速排序(實例代碼)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • springboot+Oauth2實現(xiàn)自定義AuthenticationManager和認(rèn)證path

    springboot+Oauth2實現(xiàn)自定義AuthenticationManager和認(rèn)證path

    本篇文章主要介紹了springboot+Oauth2實現(xiàn)自定義AuthenticationManager和認(rèn)證path,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • 封裝jndi操作ldap服務(wù)器的工具類

    封裝jndi操作ldap服務(wù)器的工具類

    這篇文章主要介紹了封裝JNDI操作LDAP服務(wù)器的工具類,使用者只需要會使用List,Map 數(shù)據(jù)結(jié)構(gòu),大家參考使用吧
    2014-01-01
  • 如何使用Playwright對Java API實現(xiàn)自動視覺測試

    如何使用Playwright對Java API實現(xiàn)自動視覺測試

    這篇文章主要介紹了如何使用Playwright對Java API實現(xiàn)自動視覺測試,幫助大家更好的理解和使用Playwright,感興趣的朋友可以了解下
    2021-01-01
  • Mybatis中@Param注解的作用說明

    Mybatis中@Param注解的作用說明

    這篇文章主要介紹了Mybatis中@Param注解的作用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java多線程(單例模式,堵塞隊列,定時器)詳解

    Java多線程(單例模式,堵塞隊列,定時器)詳解

    這篇文章主要介紹了java多線程的(單例模式,堵塞隊列,定時器),具有一定參考價值,加深多線程編程的理解還是很有幫助的,需要的朋友可以參考下
    2021-08-08
  • 排序算法圖解之Java希爾排序

    排序算法圖解之Java希爾排序

    希爾排序是希爾(Donald?Shell)于1959年提出的一種排序算法,其也是一種特殊的插入排序,即將簡單的插入排序進行改進后的一個更加高效的版本,也稱縮小增量排序。本文通過圖片和示例講解了希爾排序的實現(xiàn),需要的可以了解一下
    2022-11-11

最新評論

刚察县| 锡林郭勒盟| 遂溪县| 阳城县| 噶尔县| 潞西市| 凤台县| 香格里拉县| 洪雅县| 三河市| 洛阳市| 咸阳市| 中阳县| 夏邑县| 博兴县| 策勒县| 休宁县| 彰武县| 铁岭县| 望奎县| 岱山县| 奉节县| 崇州市| 通州市| 隆化县| 尼玛县| 台江县| 施甸县| 桂林市| 资溪县| 通榆县| 泰和县| 堆龙德庆县| 新源县| 秦安县| 青浦区| 天津市| 常州市| 古浪县| 侯马市| 吉林省|