Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Same column is added more than once with different values for isInsertable" error in Hibernate Spring Boot

I have a spring boot application which was in spring boot 1.2. I am now upgrading it to spring boot 2.1.x. During this upgrade I encountered the error "Same column is added more than once with different values for isInsertable".

I had the following code in one of my entities:

@MapsId("campaignMonthlyInfoId")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({@JoinColumn(name = "campaign_id", referencedColumnName = "id", insertable = false,
                            updatable = false),
                   @JoinColumn(name = "month", referencedColumnName = "month", insertable = false,
                               updatable = false),
                   @JoinColumn(name = "year", referencedColumnName = "year", insertable = false,
                               updatable = false)})
private CampaignMonthlyInfo campaignMonthlyInfo;

@EmbeddedId
CampaignDspMonthlyInfoId campaignDspMonthlyInfoId;

When I remove this from my entity the application works fine. but I need to keep this in my code.

CampaignMonthlyInfoId Entity:

@Embeddable
public class CampaignDspMonthlyInfoId implements Serializable {

  @Column
  @Enumerated(EnumType.STRING)
  Dsp dsp;

  @Embedded
  CampaignMonthlyInfoId campaignMonthlyInfoId;

  public CampaignDspMonthlyInfoId() {
  }

  public CampaignDspMonthlyInfoId(final Dsp dsp, final CampaignMonthlyInfoId id) {
    this.dsp = dsp;
    this.campaignMonthlyInfoId = id;
  }

  public Dsp getDsp() {
    return dsp;
  }

  public void setDsp(final Dsp dsp) {
    this.dsp = dsp;
  }

  public CampaignMonthlyInfoId getCampaignMonthlyInfoId() {
    return campaignMonthlyInfoId;
  }

  public void setCampaignMonthlyInfoId(final CampaignMonthlyInfoId campaignMonthlyInfoId) {
    this.campaignMonthlyInfoId = campaignMonthlyInfoId;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || getClass() != o.getClass())
      return false;
    CampaignDspMonthlyInfoId that = (CampaignDspMonthlyInfoId) o;
    return dsp == that.dsp && Objects.equal(campaignMonthlyInfoId, that.campaignMonthlyInfoId);
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(dsp, campaignMonthlyInfoId);
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this).add("dsp", dsp)
        .add("campaignMonthlyInfoId", campaignMonthlyInfoId).toString();
  }
}

CampaignMonthlyInfoId:

@Embeddable
public class CampaignMonthlyInfoId implements Serializable {

  long id;
  Month month;
  int year;


  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public Month getMonth() {
    return month;
  }

  public void setMonth(Month month) {
    this.month = month;
  }

  public int getYear() {
    return year;
  }

  public void setYear(int year) {
    this.year = year;
  }

  public CampaignMonthlyInfoId() {

  }

  public CampaignMonthlyInfoId(long id, Month month, int year) {
    this.id = id;
    this.month = month;
    this.year = year;
  }

  @Override
  public String toString() {
    return "CampaignMonthlyInfoId{" + "id=" + id + ", month=" + month + ", year=" + year + '}';
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    CampaignMonthlyInfoId that = (CampaignMonthlyInfoId) o;

    if (id != that.id) {
      return false;
    }
    if (year != that.year) {
      return false;
    }
    return month == that.month;
  }

  @Override
  public int hashCode() {
    int result = (int) (id ^ (id >>> 32));
    result = 31 * result + month.hashCode();
    result = 31 * result + year;
    return result;
  }
}
like image 475
Siddhant Sorann Avatar asked Sep 04 '25 17:09

Siddhant Sorann


1 Answers

I fixed this by setting both insertable & updatable to TRUE in the @JoinColumn annotations for each column. Problem seems to have gone away although I don't understand the source of the issue.

like image 102
Brian Blank Avatar answered Sep 06 '25 15:09

Brian Blank