首页 > 解决方案 > Embeddable 类中的重复列抛出错误

问题描述

我使用 JPA 工具创建了实体类,它为没有主键的表创建了两个类。一个是@Entity 类,另一个是@Embeddable 类,下面是这两个类的代码

国家语言

package com.geolocation.entities;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the countrylanguage database table.
 * 
 */
@Entity
@NamedQuery(name="Countrylanguage.findAll", query="SELECT c FROM Countrylanguage c")
public class Countrylanguage implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private CountrylanguagePK id;


    private String isOfficial;

    private String language;


    private float percentage;

    //bi-directional many-to-one association to Country
    
    @ManyToOne
    @JoinColumn(name="CountryCode")
    private Country country;

    public Countrylanguage() {
    }

//  public CountrylanguagePK getId() {
//      return this.id;
//  }
//
//  public void setId(CountrylanguagePK id) {
//      this.id = id;
//  }

    public String getIsOfficial() {
        return this.isOfficial;
    }

    public void setIsOfficial(String isOfficial) {
        this.isOfficial = isOfficial;
    }


    public String getLanguage() {
        return this.language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }

    public float getPercentage() {
        return this.percentage;
    }

    public void setPercentage(float percentage) {
        this.percentage = percentage;
    }

    

    public Country getCountry() {
        return this.country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

}

** 国家语言PK**

package com.geolocation.entities;

import java.io.Serializable;
import javax.persistence.*;

/**
 * The primary key class for the countrylanguage database table.
 * 
 */
@Embeddable
public class CountrylanguagePK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(insertable=false, updatable=false)
    private String countryCode;

    private String language;

    public CountrylanguagePK() {
    }
    public String getCountryCode() {
        return this.countryCode;
    }
    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }
    public String getLanguage() {
        return this.language;
    }
    public void setLanguage(String language) {
        this.language = language;
    }

    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof CountrylanguagePK)) {
            return false;
        }
        CountrylanguagePK castOther = (CountrylanguagePK)other;
        return 
            this.countryCode.equals(castOther.countryCode)
            && this.language.equals(castOther.language);
    }

    public int hashCode() {
        final int prime = 31;
        int hash = 17;
        hash = hash * prime + this.countryCode.hashCode();
        hash = hash * prime + this.language.hashCode();
        
        return hash;
    }
}

执行时抛出错误..

  1. org.springframework.beans.factory.BeanCreationException:在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义名称为“entityManagerFactory”的bean创建错误:调用init方法失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; 嵌套异常是 org.hibernate.MappingException:实体映射中的重复列:com.geolocation.entities.Countrylanguage 列:country_code(应使用 insert="false" update="false" 映射)
  2. 引起:javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; 嵌套异常是 org.hibernate.MappingException:实体映射中的重复列:com.geolocation.entities.Countrylanguage 列:country_code(应使用 insert="false" update="false" 进行映射)
  3. 引起:org.hibernate.MappingException:实体映射中的重复列:com.geolocation.entities.Countrylanguage 列:country_code(应使用 insert="false" update="false" 进行映射)

标签: javaspringhibernatespring-data-jpaembeddable

解决方案


嵌入类型不应该使用insertable = false, updatable = false,而是关联@JoinColumn的。@ManyToOne


推荐阅读