代码之家  ›  专栏  ›  技术社区  ›  super.t

在Hibernate中将列名用双引号括起来

  •  1
  • super.t  · 技术社区  · 6 年前

    我正在使用SpringJPA2.0.9和Hibernate5.3.5以及 this dialect 要通过JDBC访问FileMaker(v16)数据库,请使用本文中的官方JDBC驱动程序 distribution .

    问题是,结果SQL的列名以相应的表名为前缀,例如:

    select 
    marketingc0_.a__IDPK_MarketingCategory as a__IDPK_1_0_0_, marketingc0_.Active as Active2_0_0_
    from MarketingCategories as marketingc0_
    where marketingc0_.a__IDPK_MarketingCategory=1
    

    哪个文件制造商不接受抱怨语法错误:

    查询语法错误。

    select
        marketingc0_."a__IDPK_MarketingCategory" as a__IDPK_1_0_0_, marketingc0_."Active" as Active2_0_0_
        from MarketingCategories as marketingc0_
        where marketingc0_.a__IDPK_MarketingCategory=1
    

    我提出的解决方案是将这些引号包含到实体注释中:

    public class MarketingCategory {
        @Id
        @Column(name = "\"a__IDPK_MarketingCategory\"")
        private Integer id;
    
        @Column(name = "\"a_ID_User\"")
        private Integer userId;
    
        @Column(name = "\"Active\"")
        private Boolean active;
    
    ...
    }
    

    看起来不太好。

    是否可以配置Hibernate,使其自动将所有列名用双引号括起来?

    1 回复  |  直到 6 年前
        1
  •  6
  •   coladict    6 年前

    可以设置名为 hibernate.globally_quoted_identifiers true 它将引用所有标识符。除此之外,还有一个不引用列的选项,但是没有只引用列的选项。

        2
  •  1
  •   Fırat Küçük    5 年前

    hibernate.globally_quoted_identifiers hibernate.keyword_auto_quoting_enabled keyword_auto_quoting_enabled 自动为保留关键字添加引号。

    "BOOLEAN" "UUID" 在postgresql中。所以我修改了物理命名策略。

    hibernate.naming.physical-strategy = com.mypackage.MyCustomPhysicalNamingStrategyImpl
    

    public class MyCustomPhysicalNamingStrategyImpl implements PhysicalNamingStrategy, Serializable {
    
        public static final MyCustomPhysicalNamingStrategyImpl INSTANCE = new MyCustomPhysicalNamingStrategyImpl();
    
        @Override
        public Identifier toPhysicalCatalogName(final Identifier name, final JdbcEnvironment context) {
    
            return new Identifier(name.getText(), true);
        }
    
        @Override
        public Identifier toPhysicalSchemaName(final Identifier name, final JdbcEnvironment context) {
    
            return new Identifier(name.getText(), true);
        }
    
        @Override
        public Identifier toPhysicalTableName(final Identifier name, final JdbcEnvironment context) {
    
            return new Identifier(name.getText(), true);
        }
    
        @Override
        public Identifier toPhysicalSequenceName(final Identifier name, final JdbcEnvironment context) {
    
            return new Identifier(name.getText(), true);
        }
    
        @Override
        public Identifier toPhysicalColumnName(final Identifier name, final JdbcEnvironment context) {
    
            return new Identifier(name.getText(), true);
        }
    }
    

    Hibernate不会修改列定义,而是保持表和列名不变。

    示例实体:

    @Data
    @Entity
    @Table(name = "CHAT")
    public class Chat {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "ID", nullable = false)
        private long id;
    
        @Column(name = "UUID", columnDefinition = "UUID", nullable = false, length = 16)
        private UUID uuid;
    
        @Column(name = "NAME", length = 16)
        private String name;
    }
    

    命名将按原样进行:

    CHAT
    ------------
    ID
    UUID
    NAME
    

    更新:Hibernate5.2已经发布 GLOBALLY_QUOTED_IDENTIFIERS_SKIP_COLUMN_DEFINITIONS