shell mysql 反引号_Hibernate:对MySQL使用反引号,但对HSQL不使用反引号

bd96500e110b49cbb3cd949968f18be7.png

A project I work on (which uses Java, Spring, Hibernate) recently changed from Oracle to MySQL. There are a few cases where some of the properties in the code are reserved words in MySQL, such as "release".

There are a few solutions, 1) rename properties in the code and subsequent getter/setter methods, also update code that invokes those methods 2) annotate the property in the code with @Column(name="`release`"). This tells hibernate to quote the name when talking to the database.

I'd prefer to stay away from the first approach to reduce the chance of breaking more stuff. The second approach is "ok", except it becomes MySQL specific. In our dev. setup we use HSQL which doesn't like the backticks around those column names.

I looked at the org.hibernate.mapping.Column class and I see it has "getQuotedName" methods that I could potentially override if I could subclass Column and tell Hibernate to use my own Column class.

What's the best way to resolve this issue based on the preferred approach of a) not having to refactor the codebase (b/c of changing property names, getter/setter methods, etc) and b) wanting the app to still work in HSQL and MySQL.

It would be reasonable to have a property in properties file that could be toggled to switch on/off some Column naming fix. Which reminds me, I tried using a custom naming strategy and overriding the "columnName" method to surround the column name in backticks...this doesn't work, even on MySQL.

解决方案

The back ticks solution sounds good. But if it does not work or you do not want to use an undocumented feature of an specific JPA providery: Why don't use column names that are not reserved in any(or the most common) databases at all.

You don't need to change the name of your java properties, you must only specify a column name for them.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在JPA中,HQL(Hibernate Query Language)是一种面向对象的查询语言,用于查询数据库中的实体对象。HQL与SQL语法类似,但是使用实体类和属性名代替表名和列名。在JPA中,可以使用@Query注解来编写HQL查询。 例如,在使用JPA的@Repository注解标注的接口中,可以使用@Query注解来编写HQL查询语句。在@Query注解中,可以使用实体类和属性名来代替表名和列名。同时,可以使用参数占位符(如:name)来传递参数。 下面是一个使用HQL查询的示例代码: ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { @Query("SELECT u FROM User u WHERE u.email = :email") User findByEmail(@Param("email") String email); } ``` 在上述示例中,使用@Query注解编写了一个HQL查询语句,查询email属性等于传入参数email的User对象。 需要注意的是,HQL查询语句中的实体类和属性名是区分大小写的,且与数据库表和列名无关。因此,在编写HQL查询时,需要确保实体类和属性名的正确性。 希望以上信息对您有所帮助。\[1\]\[3\] #### 引用[.reference_title] - *1* [Spring Data JPA的使用。](https://blog.csdn.net/liuno0/article/details/124979302)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [JPA中使用delete踩坑记录](https://blog.csdn.net/qq_43680542/article/details/109724364)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Spring访问数据库之使用JPA](https://blog.csdn.net/zy199701/article/details/113833903)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值