[How to Solve]Repeated column in mapping for entity,should be mapped with insert=“false“ update=“false“

This blog post records a bug encountered in the actual development of the project.

Error background and details

The background of the problem is when a field in the database is of type longtext and JPA mapping is used

   /**
     * Product Introduction Related URL
     */
    @Basic(fetch=LAZY)
    @Lob
    @Column(name = "goods_introduce",columnDefinition = "longtext")
    private String goodsIntroduceUrl;

The error is as follows:

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

The detailed error information is as follows:

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

Solution

The problem is that a long text type field is customized in the database, and then spring data JPA customizes a type, causing duplicate fields.

The solution is very simple, that is to disable the functions of inserting and updating table fields in spring data JPA for this field.

   /**
     * Product Introduction Related URL
     */
    @Basic(fetch=LAZY)
    @Lob
    @Column(name = "goods_introduce",columnDefinition = "longtext",insertable = false,updatable = false)
    private String goodsIntroduceUrl;

Read More: