JavaSpringBootHibernate5忽略@Table和@Column
这让我快发疯了
我正在实现SpringSocial,它要求您拥有一个名为UserConnection
的数据库表(而不是使用标准命名约定,即使用下划线分隔两个单词)
因此,在我天真的世界观中,我认为通过指定@Table(name="UserConnection")
可以很容易地解决这个问题。。。但是不,那太容易了
注释被忽略,表被创建为user_connection
,这将导致SpringSocial产生hissy拟合
请告诉我,有一种简单的方法可以告诉我的Spring Boot应用程序只命名一个表(及其对应的列),使用驼峰大小写命名约定而不是标准的命名约定
# 1 楼答案
选项1
首先,在@Entity映射中定义表名:
选项2
你应该用NamingStrategy支付一点费用。为sessionFactory bean定义属性时,请尝试添加以下内容:
因此,如果不想为每个实体显式命名表名,则应遵循此策略
选项3
或者,如果上述方法不适用于您,您必须使用PhysicalNaming策略。虽然这是你最后的选择:
参考:https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/chapters/domain/naming.html
# 2 楼答案
TL;DR
将以下内容添加到
application.yml
文件中:或者你的
application.properties
:详细答案
正如Spring Boot 1.4 release notes所述:
这个新的
PhysicalNamingStrategy
遵循Spring推荐的命名约定。无论如何,如果你想完全控制物理命名,最好使用org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
。您可以通过在application.yml
中添加以下内容来切换到该命名策略:SpringPhysicalNamingStrategy
的^{它基本上用下划线替换任何} 方法)
.
和大小写更改(看看^{