Hibernate抛出类广播异常,因为@JoinFormula无法转换为@Column。我该如何解决?

时间:2019-05-18 07:35:12

标签: hibernate spring-boot jpa join hibernate-mapping

我正在研究现有的spring-boot项目。应用程序的雇员实体与其他78个实体映射。每个实体都有组合键。当我运行项目时,我在这里收到错误消息:-

  @ManyToOne
  @JoinColumns({
  @JoinColumn(name="COMPANYID", referencedColumnName="COMPANYID", insertable=false, updatable=false),
  @JoinColumn(name="COMPANYFIRM", referencedColumnName="COMPANYFIRM")  })
  private Firm firm;

Firm.java

public class Firm
  implements Serializable
{

  @EmbeddedId
  private FirmID firmID;

  private String description;
}

FirmID.java

package com.nicholas.jpa;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class FirmID
    implements Serializable
{
  @Column(name="COMPANYID")
  private int id;

  @Column(name="COMPANYFIRM")
  private String name;

  public FirmID()
    {
    }

    public FirmID(int id, String name)
    {
        this.id = id;
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getId()
    {
        return id;
    }

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

    public int hashCode()
    {
        return name.hashCode();
    }

    @Override
    public String toString()
    {
        return String.format("%d-%s", id, name);
    }

    public boolean equals(Object obj)
    {
        if (obj == this)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof FirmID))
            return false;

        FirmID pk = (FirmID) obj;
        return pk.id == id && pk.name.equals(name);
    }
}

错误是:-

  

由以下原因引起:org.hibernate.AnnotationException:不允许在属性中混合可插入和不可插入的列:com.nicholas.jpa.Employee.firm       在org.hibernate.cfg.Ejb3Column.checkPropertyConsistency(Ejb3Column.java:718)〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]       在org.hibernate.cfg.AnnotationBinder.bindManyToOne(AnnotationBinder.java:3035)〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]       在org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1776)〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]       在org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:924)〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]       在org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:751)〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]

我通过使用Should Hibernate be able to handle overlapping foreign keys?

解决了这个问题
@ManyToOne(optional = true)
@JoinColumnsOrFormulas(value = {
        @JoinColumnOrFormula(formula = @JoinFormula(referencedColumnName = "COMPANYID", value = "COMPANYID")),
        @JoinColumnOrFormula(formula = @JoinFormula(referencedColumnName = "COMPANYFIRM", value = "COMPANYFIRM"))})
private Firm firm;

现在上述错误消失了,所以我将所有@JoinColumns替换为@JoinColumnsOrFormulas。最后,我遇到了ClassCastException

  

原因:java.lang.ClassCastException:org.hibernate.mapping.Formula无法转换为org.hibernate.mapping.Column       在org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:584)〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]       在org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1627)〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]       在org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:881)〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]       在org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:796)〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]       在org.hibernate.cfg.annotations.CollectionBinder $ 1.secondPass(CollectionBinder.java:735)〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]       在org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)〜[hibernate-core-5.2.16.Final.jar:5.2.16.Final]

正如我已经提到的,有很多实体,而且我已经检查了每一行的每个实体,没有错误。 名为EntryExit.java的实体是替换@JoinColumns的最后一个实体,如果我替换了它们,则此ClassCastException引发其他明智的旧“不允许在属性中混合可插入和不可插入的列”异常。

我检查了以下答案,但在我的情况下似乎没有任何效果:-

  1. hibernate @JoinColumnsOrFormulas throws Formula cannot be cast to Column exception

  2. Hibernate and @JoinFormula: org.hibernate.mapping.Formula cannot be cast to org.hibernate.mapping.Column

  3. https://hibernate.atlassian.net/browse/HHH-10897

请建议我是否有另一种方法可以在现有方法中使用复合键或解决方案映射@ManyToOne。我在等待您的帮助..!

0 个答案:

没有答案