栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

休眠如何生成外键约束名称?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

休眠如何生成外键约束名称?

Hibernate通过串联表和属性名称来生成约束名称,并将结果转换为

MD5
。由于某些数据库中的约束名称长度限制,因此需要它。例如,在Oracle数据库中,外键名称的长度不能超过30个符号长度。

Hibernate源org.hibernate.mapping.Constraint中的此代码段

public static String generateName(String prefix, Table table, Column... columns) {    // Use a concatenation that guarantees uniqueness, even if identical names    // exist between all table and column identifiers.    StringBuilder sb = new StringBuilder( "table`" + table.getName() + "`" );    // Ensure a consistent ordering of columns, regardless of the order    // they were bound.    // Clone the list, as sometimes a set of order-dependent Column    // bindings are given.    Column[] alphabeticalColumns = columns.clone();    Arrays.sort( alphabeticalColumns, ColumnComparator.INSTANCE );    for ( Column column : alphabeticalColumns ) {        String columnName = column == null ? "" : column.getName();        sb.append( "column`" ).append( columnName ).append( "`" );    }    return prefix + hashedName( sb.toString() );}public static String hashedName(String s) {    try {        MessageDigest md = MessageDigest.getInstance( "MD5" );        md.reset();        md.update( s.getBytes() );        byte[] digest = md.digest();        BigInteger bigInt = new BigInteger( 1, digest );        // By converting to base 35 (full alphanumeric), we guarantee        // that the length of the name will always be smaller than the 30        // character identifier restriction enforced by a few dialects.        return bigInt.toString( 35 );    }    catch ( NoSuchAlgorithmException e ) {        throw new HibernateException( "Unable to generate a hashed Constraint name!", e );    }}

您可以使用生成自己的约束名称(唯一键和外键)

ImplicitNamingStrategy
。您可以参考Hibernate5NamingStrategy作为示例。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/387375.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号