异常org.hibernate.DuplicateMappingException:Table [xx] contains physical column name [xx]

问题描述:

Caused by: org.hibernate.DuplicateMappingException: Table [jst_order] contains physical column name [is_cod] referred to by multiple logical column names: [is_Cod], [isCod]
错误信息翻译:重复映射异常,is_cod字段在表 jst_order中对应了is_Cod与isCod多个字段。
项目描述:SpringBoot+Hibernate项目中:实体类中属性上加入@Colunm注解,并指定注解的name为对应表字段时,报该错误。当将@Colunm注解去除或是在name属性中填入当前实体类名称,则不报错。
异常org.hibernate.DuplicateMappingException:Table [xx] contains physical column name [xx]_第1张图片
在这里插入图片描述
详细错误信息如下:

Caused by: org.hibernate.DuplicateMappingException: Table [jst_order] contains physical column name [is_cod] referred to by multiple logical column names: [is_Cod], [isCod]
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl$TableColumnNameBinding.bindPhysicalToLogical(InFlightMetadataCollectorImpl.java:931)
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl$TableColumnNameBinding.addBinding(InFlightMetadataCollectorImpl.java:900)
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addColumnNameBinding(InFlightMetadataCollectorImpl.java:970)
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addColumnNameBinding(InFlightMetadataCollectorImpl.java:951)
	at org.hibernate.cfg.Ejb3Column.addColumnBinding(Ejb3Column.java:409)
	at org.hibernate.cfg.Ejb3Column.linkWithValue(Ejb3Column.java:371)
	at org.hibernate.cfg.annotations.SimpleValueBinder.linkWithValue(SimpleValueBinder.java:450)
	at org.hibernate.cfg.annotations.SimpleValueBinder.make(SimpleValueBinder.java:426)
	at org.hibernate.cfg.annotations.PropertyBinder.makePropertyAndValue(PropertyBinder.java:193)
	at org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:205)
	at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2282)
	at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:975)
	at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:802)
	at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:254)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:230)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:273)
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1214)
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1245)
	at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391)
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378)
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1862)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1799)
	... 16 common frames omitted

原因分析:

应用Spring Data JPA 自定创建数据表时,设置的字段命名策略为 `SpringPhysicalNamingStrategy`
jpa:
   hibernate:
   	   naming:
   	   	   physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy

这个命名策略:当字段为小写,当有大写字母的时候会转换为分隔符号“_”,所以isCod就会自动创建一个is_cod的字段属性,如果我们制定@Column时,还给它指定is_cod它就会认为我们给该实体类属性指定了多个数据表字段。


解决方案:

方案一:去掉@Column注解
异常org.hibernate.DuplicateMappingException:Table [xx] contains physical column name [xx]_第2张图片

方案二:修改命名策略

  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

PhysicalNamingStrategyStandardImpl 直接映射,不做过多的处理,但存在@Table与@Column注解时,以设置的@Table,@Column的属性为准。

暂时只找到这些办法,如果你有好的方法,欢迎留言。

你可能感兴趣的:(jpa,hibernate,spring,boot,java)