在休眠中为同一实体的 MySQL 和 SQL 服务器配置差异 table 名称
Configure diff table name in hibernate for MySQL and SQL server for the same entity
我有一个 table 名为“交易”的实体。使用 spring orm 和 hibernate 创建连接时,在启动时它成功地在 MySQL 中创建了 tables 但作为 SQL 服务器中的关键字“Transaction”,它在启动时失败。
我无法更改代码中的 table 名称,因为有很多连锁反应。因此,有没有一种方法可以将 table 名称定义为 java 中的事务,并以这样一种方式进行拦截或配置,即在连接到 SQL 服务器时休眠会转义关键字并将其查询为“[Transaction]”(使用 [] 转义)
您可以尝试以下方法之一:
@Table(name = "`Transaction`");
@Table(name = "\"Transaction\"")
或通过设置 属性:
hibernate.auto_quote_keyword=true
您可以使用本文所述的命名策略:https://www.baeldung.com/hibernate-naming-strategy
public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy {
@Override
public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return Identifier.toIdentifier(**your table name**);
}
// Other methods
我有一个 table 名为“交易”的实体。使用 spring orm 和 hibernate 创建连接时,在启动时它成功地在 MySQL 中创建了 tables 但作为 SQL 服务器中的关键字“Transaction”,它在启动时失败。 我无法更改代码中的 table 名称,因为有很多连锁反应。因此,有没有一种方法可以将 table 名称定义为 java 中的事务,并以这样一种方式进行拦截或配置,即在连接到 SQL 服务器时休眠会转义关键字并将其查询为“[Transaction]”(使用 [] 转义)
您可以尝试以下方法之一:
@Table(name = "`Transaction`");
@Table(name = "\"Transaction\"")
或通过设置 属性:
hibernate.auto_quote_keyword=true
您可以使用本文所述的命名策略:https://www.baeldung.com/hibernate-naming-strategy
public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy {
@Override
public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return Identifier.toIdentifier(**your table name**);
}
// Other methods