Hibernate注释referencedcolumnname不起作用,referencedcolumnname自动分配给另一个实体的主键

时间:2015-07-01 06:27:11

标签: java hibernate

在enthesaplasmaek实体中,我与我的两个实体EntHesaplasma和EntHesaplasmaek建立了如下关系

@OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="hesaplasmaekid",referencedColumnName="carihareketid",insertable=false,updatable=false)
    @Where(clause = "enthesaplasma.evraktip='EK'")
    public EntHesaplasma getEnthesaplasma() {
        return enthesaplasma;
    }

当我运行下面的hql时,hibernate在hesaplasmaekid和hesaplasmaid(enthesaplasma实体的主键(@Id))之间建立了关系。但正确的关系应该是在enthesaplasma实体中的carihareketid和enthesaplasmaek实体中的enthesaplasmaekid之间。

select hs.hesaplasmaid,hs.carihareketid,ek.hesaplasmaekid,hs.hesaplasmano,ek.aciklama from EntHesaplasmaek ek left join ek.enthesaplasma hs

查询结果(来自eclipse中的Hibernate查询编辑器)

result of the query

我该如何更正此错误?感谢。

我的实体位于

之下

EntHesaplasma实体

@Entity
@Table(name = "XOZ_HESAPLASMA")
public class EntHesaplasma {

    Integer hesaplasmaid;
    @Transient
    EntCariHareketler carihareket;
    @Resolvable(colName = "Hesaplaşma No",formatter=FormatterInteger.class)
    Integer hesaplasmano;
    Integer hesaplasmagrupid;
    Date datecreated;
    Integer carihareketid;
    String evraktip;
    Boolean isactive;
    Double meblag;
    EntHesaplasmagrup enthesaplasmagrup;
    EntHesaplasmaek enthesaplasmaek;

    @Id
    @GeneratedValue
    @Column(name = "hesaplasmaid", unique = true, nullable = false)
    public Integer getHesaplasmaid() {
        return hesaplasmaid;
    }

    public void setHesaplasmaid(Integer hesaplasmaid) {
        this.hesaplasmaid = hesaplasmaid;
    }

    @Column(name = "datecreated")
    public Date getDatecreated() {
        return datecreated;
    }

    public void setDatecreated(Date datecreated) {
        this.datecreated = datecreated;
    }

    @Column(name = "hesaplasmano")
    public Integer getHesaplasmano() {
        return hesaplasmano;
    }

    //@OneToOne(fetch= FetchType.LAZY)
    //@JoinColumn(name="carihareketid")
    @Transient
    public EntCariHareketler getCarihareket() {
        return carihareket;
    }

    public void setCarihareket(EntCariHareketler carihareket) {
        this.carihareket = carihareket;
    }


    public void setHesaplasmano(Integer hesaplasmano) {
        this.hesaplasmano = hesaplasmano;
    }

    @Column(name = "carihareketid")
    public Integer getCarihareketid() {
        return carihareketid;
    }

    public void setCarihareketid(Integer carihareketid) {
        this.carihareketid = carihareketid;
    }

    @Column(name="isactive")
    public Boolean getIsactive() {
        return isactive;
    }

    public void setIsactive(Boolean isactive) {
        this.isactive = isactive;
    }

    @Column(name="meblag")
    public Double getMeblag() {
        return meblag;
    }

    public void setMeblag(Double meblag) {
        this.meblag = meblag;
    }

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="hesaplasmagrupid")
    public EntHesaplasmagrup getEnthesaplasmagrup() {
        return enthesaplasmagrup;
    }

    public void setEnthesaplasmagrup(EntHesaplasmagrup enthesaplasmagrup) {
        this.enthesaplasmagrup = enthesaplasmagrup;
    }

    @Column(name="hesaplasmagrupid",insertable=false,updatable=false)
    public Integer getHesaplasmagrupid() {
        return hesaplasmagrupid;
    }

    public void setHesaplasmagrupid(Integer hesaplasmagrupid) {
        this.hesaplasmagrupid = hesaplasmagrupid;
    }

    @Column(name="evraktip")
    public String getEvraktip() {
        return evraktip;
    }

    public void setEvraktip(String evraktip) {
        this.evraktip = evraktip;
    }

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="carihareketid",referencedColumnName="hesaplasmaekid",insertable=false,updatable=false)
    @Where(clause = "evraktip='EK'")
    public EntHesaplasmaek getEnthesaplasmaek() {
        return enthesaplasmaek;
    }

    public void setEnthesaplasmaek(EntHesaplasmaek enthesaplasmaek) {
        this.enthesaplasmaek = enthesaplasmaek;
    }




}

EntHesaplasmaek实体

@Entity
@Table(name = "XOZ_HESAPLASMAEK")
public class EntHesaplasmaek {

    Integer hesaplasmaekid;
    @Resolvable(colName="Bakiye",formatter=DoubleFormatter.class)
    Double bakiye;
    @Resolvable(colName="Açıklama")
    String aciklama;
    Integer konuid;
    Boolean boolzamanlanmis;
    Integer zamanid;
    Date datecreated;
    @Resolvable(colName="Tarih",formatter=DateFormatterTbl.class)
    Date evraktarih;
    @Resolvable(colName="Hesaplasma No",formatter=FormatterInteger.class)
    @Transient
    Integer hesaplasmano;
    Integer hesaplasmagrupid;
    EntHesaplasmagrup enthesaplasmagrup;
    EntHesaplasma enthesaplasma;

    @Id
    @GeneratedValue
    @Column(name = "hesaplasmaekid", unique = true, nullable = false)
    public Integer getHesaplasmaekid() {
        return hesaplasmaekid;
    }
    public void setHesaplasmaekid(Integer hesaplasmaekid) {
        this.hesaplasmaekid = hesaplasmaekid;
    }

    @Column(name="bakiye")
    public Double getBakiye() {
        return bakiye;
    }
    public void setBakiye(Double bakiye) {
        this.bakiye = bakiye;
    }

    @Column(name="aciklama")
    public String getAciklama() {
        return aciklama;
    }
    public void setAciklama(String aciklama) {
        this.aciklama = aciklama;
    }

    @Column(name="konuid")
    public Integer getKonuid() {
        return konuid;
    }
    public void setKonuid(Integer konuid) {
        this.konuid = konuid;
    }

    @Column(name="boolzamanlanmis")
    public Boolean getBoolzamanlanmis() {
        return boolzamanlanmis;
    }
    public void setBoolzamanlanmis(Boolean boolzamanlanmis) {
        this.boolzamanlanmis = boolzamanlanmis;
    }

    @Column(name="zamanid")
    public Integer getZamanid() {
        return zamanid;
    }
    public void setZamanid(Integer zamanid) {
        this.zamanid = zamanid;
    }

    @Column(name="datecreated")
    public Date getDatecreated() {
        return datecreated;
    }
    public void setDatecreated(Date datecreated) {
        this.datecreated = datecreated;
    }

    @Column(name="evraktarih")
    public Date getEvraktarih() {
        return evraktarih;
    }
    public void setEvraktarih(Date evraktarih) {
        this.evraktarih = evraktarih;
    }

    @Transient
    public Integer getHesaplasmano() {
        return hesaplasmano;
    }
    public void setHesaplasmano(Integer hesaplasmano) {
        this.hesaplasmano = hesaplasmano;
    }

    @Column(name="hesaplasmagrupid",insertable=false,updatable=false)
    public Integer getHesaplasmagrupid() {
        return hesaplasmagrupid;
    }
    public void setHesaplasmagrupid(Integer hesaplasmagrupid) {
        this.hesaplasmagrupid = hesaplasmagrupid;
    }

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="hesaplasmagrupid")
    public EntHesaplasmagrup getEnthesaplasmagrup() {
        return enthesaplasmagrup;
    }

    public void setEnthesaplasmagrup(EntHesaplasmagrup enthesaplasmagrup) {
        this.enthesaplasmagrup = enthesaplasmagrup;
    }

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="hesaplasmaekid",referencedColumnName="carihareketid",insertable=false,updatable=false)
    @Where(clause = "enthesaplasma.evraktip='EK'")
    public EntHesaplasma getEnthesaplasma() {
        return enthesaplasma;
    }
    public void setEnthesaplasma(EntHesaplasma enthesaplasma) {
        this.enthesaplasma = enthesaplasma;
    }



}

2 个答案:

答案 0 :(得分:1)

有点晚了,但是遇到了同样的问题。 不能设置双向关系不是一个bug,一侧需要使用JoinColumn,另一侧需要通过mappingBy对其进行映射。所以是的。这里的解决方案是正确的!

我在这里找到了解释: How do I join tables on non-primary key columns?

答案 1 :(得分:0)

这似乎是hibernate中的一个错误。但我用mappedby注释解决了我的问题。

我在我的enthesaplasma实体中添加了一个字段,如下所示。我在这个实体中定义了这种关系。

match

我更新了我的其他实体,如下所示。

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="carihareketid",insertable=false,updatable=false)
    public EntHesaplasmaek getEnthesaplasmaek() {
        return enthesaplasmaek;
    }

    public void setEnthesaplasmaek(EntHesaplasmaek enthesaplasmaek) {
        this.enthesaplasmaek = enthesaplasmaek;
    }

添加信息和问题:在此链接中,当实体中存在复合P.K时,表示使用referencedcolumnname。这是对的吗?

(来自What is referencedColumnName used for in JPA?

在referencedColumnName上引用API:

  

此外键列引用的列的名称。        默认值(仅在使用单个连接列时适用):与引用表的主键列相同。

Q / A

会在哪里使用?

  

当引用表中有“复合PK”时,则需要   指定要引用的列名。