异常org.hibernate.DuplicateMappingException:Table [xx] contains physical column name [xx]
程序员文章站
2022-04-11 17:27:56
...
问题描述:
Caused by: org.hibernate.DuplicateMappingException: Table [postrolerelation] contains logical column name [SORTVALUE] referring to multiple physical column names: [sort_value], [sortvalue]
错误信息翻译:表[postrolerelation]包含逻辑列名[SORTVALUE],指多个物理列名:[sort_value], [SORTVALUE]。
项目描述:SpringBoot+H2写单元测试,postrolerelation表中以id和sortValue作为联合主键。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: Table [postrolerelation] contains logical column name [SORTVALUE] referring to multiple physical column names: [sort_value], [sortvalue]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at dev.csi.organ.OrganInfoApplication.main(OrganInfoApplication.java:24) [test-classes/:na]
Caused by: org.hibernate.DuplicateMappingException: Table [postrolerelation] contains logical column name [SORTVALUE] referring to multiple physical column names: [sort_value], [sortvalue]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl$TableColumnNameBinding.bindLogicalToPhysical(InFlightMetadataCollectorImpl.java:923) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl$TableColumnNameBinding.addBinding(InFlightMetadataCollectorImpl.java:911) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addColumnNameBinding(InFlightMetadataCollectorImpl.java:982) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addColumnNameBinding(InFlightMetadataCollectorImpl.java:963) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
at org.hibernate.cfg.Ejb3Column.addColumnBinding(Ejb3Column.java:409) ~[hibernate-core-5.3.10.Final.jar:5.3.10.Final]
原因分析:
应用Spring Data JPA 自定创建数据表时,设置的字段命名策略为 `SpringPhysicalNamingStrategy`
<span style="color:#000000"><code class="language-xml">jpa:
hibernate:
naming:
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
</code></span>
这个命名策略:当字段为小写,当有大写字母的时候会转换为分隔符号“_”,所以sortValue就会自动创建一个sort_value的字段属性,如果我们制定@Column时,还给它指定sort_value它就会认为我们给该实体类属性指定了多个数据表字段。
解决方案:修改命名策略
<span style="color:#000000"><code class="language-xml">
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl</code></span>
PhysicalNamingStrategyStandardImpl 直接映射,不做过多的处理,但存在@Table与@Column注解时,以设置的@Table,@Column的属性为准。