hibernate - hibernate 如何生成外键约束名称?

标签 hibernate hbm2ddl

hibernate 如何生成外键约束名称?

如果我没有定义一个名字 hibernate 会生成这样的东西

CONSTRAINT fk_2ocepcfwpr1v18dg1ieoe6bau

这个名字是如何产生的?也许来自 MD5字段名称的散列或类似的东西?我需要知道名称是否在所有实例上都相同。

最佳答案

Hibernate 通过连接表名和属性名生成约束名,并将结果转换为 MD5 .它是必需的,因为某些数据库中的约束名称长度限制。例如,在Oracle 数据库中,外键名称长度不能超过30 个符号长度。

此代码片段来自 Hibernate 源 org.hibernate.mapping.Constraint

/**
 * If a constraint is not explicitly named, this is called to generate
 * a unique hash using the table and column names.
 * Static so the name can be generated prior to creating the Constraint.
 * They're cached, keyed by name, in multiple locations.
 *
 * @return String The generated name
 */
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() );
}

/**
 * Hash a constraint name using MD5. Convert the MD5 digest to base 35
 * (full alphanumeric), guaranteeing
 * that the length of the name will always be smaller than the 30
 * character identifier restriction enforced by a few dialects.
 * 
 * @param s
 *            The name to be hashed.
 * @return String The hased name.
 */
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 , 举个例子。

关于hibernate - hibernate 如何生成外键约束名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36766848/

相关文章:

java - 在抽象 JPA DAO 中抽象命名查询

Hibernate 4.0 hibernatetool taskdef 错误

postgresql - 未找到 all_sequences 表/关系?

java - hibernate 映射 - 运行 hibernate 配置时出现错误

java - 为什么 hbm2ddl 不喜欢我在 GregorianCalendar 上的 @Temporal 注释?

java - 如何将带有子查询的 SQL 查询转换为 JPA 标准生成器

mysql - 使用 hibernate 将枚举存储在数据库中

java - 使用 HQL 执行搜索/用户使用哪种连接类型?

sql - Grails 中的非平凡数据查询

java - 使用 HSQL 进行 Hibernate - 未使用 hbm2ddl 创建表