0

I have create entity class using JPA Tools, It has created two classes for the table that has no primary key. One is @Entity class and other is @Embeddable class, below are the code for both the classes

Countrylanguage

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

}

** CountrylanguagePK**

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

while executing it is throwing the error..

  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: com.geolocation.entities.Countrylanguage column: country_code (should be mapped with insert="false" update="false")
  2. 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.geolocation.entities.Countrylanguage column: country_code(should be mapped with insert="false" update="false")
  3. Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.geolocation.entities.Countrylanguage column: country_code(should be mapped with insert="false" update="false")
VJain
  • 1,027
  • 7
  • 17
  • 37

1 Answers1

0

The embeddable type shouldn't use insertable = false, updatable = false but the @JoinColumn of the @ManyToOne association.

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58