解决Repeated column in mapping for entity,should be mapped with insert=“false“ update=“false“

这篇博文记录项目实战开发中遇到的一个bug。

错误背景和详情

问题出现的背景是当数据库中有一个字段为longtext 类型,使用JPA 映射时候出现的这个错误

   /**
     * 商品介绍相关URL
     */
    @Basic(fetch=LAZY)
    @Lob
    @Column(name = "goods_introduce",columnDefinition = "longtext")
    private String goodsIntroduceUrl;

错误提示如下:

Repeated column in mapping for entity: com.xxx.business.goods.bean.GoodsEntity column: goods_introduce (should be mapped with insert="false" update="false")

详细错误信息如下:

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.xxx.business.goods.bean.GoodsEntity column: goods_introduce (should be mapped with insert="false" update="false")
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:403)
	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:1855)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792)
	... 16 common frames omitted
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.xxx.business.goods.bean.GoodsEntity column: goods_introduce (should be mapped with insert="false" update="false")
	at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:862)
	at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:880)
	at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:902)
	at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:634)
	at org.hibernate.mapping.RootClass.validate(RootClass.java:267)
	at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:351)
	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:464)
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1249)
	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)
	... 20 common frames omitted

解决方案

问题在于数据库中自定义了一个longtext 类型字段,然后Spring Data JPA 自定义了一种类型,造成字段重复。

解决思路也很简单,就是针对该字段禁用Spring Data JPA 的插入表字段和更新表字段的功能。

   /**
     * 商品介绍相关URL
     */
    @Basic(fetch=LAZY)
    @Lob
    @Column(name = "goods_introduce",columnDefinition = "longtext",insertable = false,updatable = false)
    private String goodsIntroduceUrl;

你可能感兴趣的:(StackOverflow,Repeated,column,spring,data,jpa,映射longtext,jpa映射text)