3

in enthesaplasmaek entity, i made relationship as below with my two entities, EntHesaplasma and EntHesaplasmaek

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

when i run the below hql , hibernate makes a relationship between hesaplasmaekid and hesaplasmaid (primary key (@Id) of enthesaplasma entity). But the correct relationship should be between carihareketid in enthesaplasma entity and enthesaplasmaekid in the enthesaplasmaek entity.

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

Result of the query (from Hibernate Query Editor in eclipse)

result of the query

how can i correct this error ? thanks.

My entities are below

EntHesaplasma entity

@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

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



}
engtuncay
  • 867
  • 10
  • 29

2 Answers2

1

A bit late, but got into the same problem. It's not a bug that you can't set up bidirectional relation, one side need to use JoinColumn, another map it via mappedBy. So yes..the solution here is correct!

I found the explanation here: How do I join tables on non-primary key columns?

Joik
  • 175
  • 7
0

this seems a bug in hibernate. but i solved my problem with mappedby annotation.

i added a field to my enthesaplasma entity as below. i defined the relationship in this entity.

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

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

i updated my other entity as below.

    @OneToOne(fetch=FetchType.LAZY,mappedBy="enthesaplasmaek")
    public EntHesaplasma getEnthesaplasma() {
        return enthesaplasma;
    }
    public void setEnthesaplasma(EntHesaplasma enthesaplasma) {
        this.enthesaplasma = enthesaplasma;
    }

Addition info and question : in this link, it is said that referencedcolumnname is used, when there is a composite P.K in an entity. is this correct ?

(from What is referencedColumnName used for in JPA? )

Quoting API on referencedColumnName:

The name of the column referenced by this foreign key column. Default (only applies if single join column is being used): The same name as the primary key column of the referenced table.

Q/A

Where this would be used?

When there is a "composite PK" in referenced table, then you need to specify column name you are referencing.

Community
  • 1
  • 1
engtuncay
  • 867
  • 10
  • 29