gpt4 book ai didi

java - 需要帮助在 Hibernate 5 中为外键列名实现 ImplicitNamingStrategy

转载 作者:行者123 更新时间:2023-11-29 04:45:58 24 4
gpt4 key购买 nike

我试图通过覆盖 ImplicitNamingStrategyJpaCompliantImpl 类中的 determineForeignKeyName 方法来为外键列名称添加下划线,但不知何故它不起作用。下面是我创建的类,

public class CustomNamingStrategy extends ImplicitNamingStrategyJpaCompliantImpl implements Serializable{

public static final CustomNamingStrategy INSTANCE=new CustomNamingStrategy ();



/**
* Produces a plural table name from the given class name
* @return a pluralized version of the class name using underscores instead of mixed case.
*/
@Override
protected String transformEntityName(EntityNaming entityNaming) {
return Noun.pluralOf(addUnderscores(StringHelper.unqualify(entityNaming.getEntityName())));
}

protected static String addUnderscores(String name) {
StringBuilder buf = new StringBuilder(name.replace('.', '_'));
for (int i = 1; i < buf.length() - 1; i++) {
if (
Character.isLowerCase(buf.charAt(i - 1)) &&
Character.isUpperCase(buf.charAt(i)) &&
Character.isLowerCase(buf.charAt(i + 1))
) {
buf.insert(i++, '_');
}
}
return buf.toString().toLowerCase(Locale.ROOT);
}

@Override
public Identifier determineForeignKeyName(ImplicitForeignKeyNameSource source) {

return toIdentifier(
NamingHelper.INSTANCE.generateHashedFkName(
"FK",
source.getTableName(),
source.getReferencedTableName(),
addUnderscorestocolumns(source.getColumnNames())
),
source.getBuildingContext()
);
}


public List<Identifier> addUnderscorestocolumns(List<Identifier> columnNamesList) {

List<Identifier> underscorecolumns = new ArrayList<Identifier>();

for (int i = 0; i < columnNamesList.size(); i++) {
underscorecolumns.add(Identifier.toIdentifier(addUnderscores(columnNamesList.get(i).getText()))) ;
}
return underscorecolumns;
}



@Override
public Identifier determineBasicColumnName(ImplicitBasicColumnNameSource source) {

return toIdentifier(transformAttributePathCustom(source.getAttributePath()), source.getBuildingContext());
}

protected String transformAttributePathCustom(AttributePath attributePath) {
return addUnderscores(attributePath.getProperty());
}

}

因此,除了 determineForeignKeyName 方法外,其他方法都按预期工作,例如为表格添加复数名称和下划线。当我调试代码时,我可以看到控件进入外键列的 determineForeignKeyName 方法并添加了下划线,但是当控件转到 InflightMetadataCollectorImpl 类(org.hibernate.boot.internal)时,我只能看到引用添加列,但不添加列名称的下划线。

我是否需要为外键列名称实现任何其他方法,或者我的实现有任何错误?请提出建议。

最佳答案

NamingHelper.INSTANCE.generateHashedFkName() 生成哈希名称。你需要这样的东西

@Override
public Identifier determineForeignKeyName(ImplicitForeignKeyNameSource source) {
return toIdentifier(
"FK_" +
source.getTableName().getText() + "_" +
source.getReferencedTableName().getText() + "_" +
addUnderscorestocolumns(source.getColumnNames()),
source.getBuildingContext());
}

你可以引用例子 Hibernate5NamingStrategy

方法Identifier determineForeignKeyName(ImplicitForeignKeyNameSource source)不是用来生成列名的,而是为这个SQL生成外键约束名,比如F_users_fk_address

alter table users 
add constraint F_users_fk_address
foreign key (fk_address)
references user_addresses (f_pid)

要指定外键列名,您需要覆盖此方法

标识符 determineJoinColumnName(ImplicitJoinColumnNameSource source)

引用这个:JoinColumnStrategyTest.java

关于java - 需要帮助在 Hibernate 5 中为外键列名实现 ImplicitNamingStrategy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37195218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com