可嵌入类中的重复列引发错误

wkftcu5l  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(248)

我使用jpa工具创建了实体类,它为没有主键的表创建了两个类。一个是@entity class,另一个是@embeddeble class,下面是这两个类的代码
国家语言

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;
    }
}

执行时抛出错误。。
org.springframework.beans.factory.beancreationexception:创建名为“entitymanagerfactory”的bean时出错,该bean在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/hibernatejpaconfiguration.class]中定义:调用init方法失败;嵌套异常为javax.persistence.persistenceexception:[persistenceunit:default]无法构建hibernate sessionfactory;嵌套异常为org.hibernate.mappingexception:repeated column in mapping for entity:com.geolocation.entities.countrylanguage column:country\u code(应使用insert=“false”update=“false”Map)
原因:javax.persistence.persistenceexception:[persistenceunit:default]无法构建hibernate sessionfactory;嵌套异常为org.hibernate.mappingexception:repeated column in mapping for entity:com.geolocation.entities.countrylanguage column:country\u code(应使用insert=“false”update=“false”Map)
原因:org.hibernate.mappingexception:实体Map中的重复列:com.geolocation.entities.countrylanguage列:国家代码(应使用insert=“false”update=“false”Map)

ibrsph3r

ibrsph3r1#

嵌入类型不应使用 insertable = false, updatable = false 但是 @JoinColumn@ManyToOne 联想。

相关问题