我有一个查询,我碰巧有一个名为“ SGOrdCompra”的表,该表将他与“ SGPersona”链接了两次,如下表:

Sgordcompra.java

@Entity
@Table(name = "`SGOrdCompra`", schema = "`public`")
public class Sgordcompra implements java.io.Serializable {

    .....
    private Sgpersona sgpersonaByIcodSolicitante;
    private Sgpersona sgpersonaByIcodComprador;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "`iCodSolicitante`", nullable = false)
    public Sgpersona getSgpersonaByIcodSolicitante() {
        return this.sgpersonaByIcodSolicitante;
    }

    public void setSgpersonaByIcodSolicitante(
            Sgpersona sgpersonaByIcodSolicitante) {
        this.sgpersonaByIcodSolicitante = sgpersonaByIcodSolicitante;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "`iCodComprador`", nullable = false)
    public Sgpersona getSgpersonaByIcodComprador() {
        return this.sgpersonaByIcodComprador;
    }

    public void setSgpersonaByIcodComprador(Sgpersona sgpersonaByIcodComprador) {
        this.sgpersonaByIcodComprador = sgpersonaByIcodComprador;
    }

}


Sgpersona.java

@Entity
@Table(name = "`SGPersona`", schema = "`public`")
public class Sgpersona implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private int icodPersona;
    private Set<Sgrencajchica> sgrencajchicas = new HashSet<Sgrencajchica>(0);
    ....
    private Set<Sgordcompra> sgordcomprasForIcodComprador = new HashSet<Sgordcompra>(0);
    private Set<Sgordcompra> sgordcomprasForIcodSolicitante = new HashSet<Sgordcompra>(0);

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "sgpersona")
    public Set<Sgrencajchica> getSgrencajchicas() {
        return this.sgrencajchicas;
    }

    public void setSgrencajchicas(Set<Sgrencajchica> sgrencajchicas) {
        this.sgrencajchicas = sgrencajchicas;
    }

    .......

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "sgpersona")
    public Set<Sgordcompra> getSgordcomprasForIcodComprador() {
        return this.sgordcomprasForIcodComprador;
    }

    public void setSgordcomprasForIcodComprador(
            Set<Sgordcompra> sgordcomprasForIcodComprador) {
        this.sgordcomprasForIcodComprador = sgordcomprasForIcodComprador;
    }



    @OneToMany(fetch = FetchType.LAZY, mappedBy = "sgpersona")
    public Set<Sgordcompra> getSgordcomprasForIcodSolicitante() {
        return this.sgordcomprasForIcodSolicitante;
    }

    public void setSgordcomprasForIcodSolicitante(
            Set<Sgordcompra> sgordcomprasForIcodSolicitante) {
        this.sgordcomprasForIcodSolicitante = sgordcomprasForIcodSolicitante;
    }

}


我收到此错误:

ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'iGenericDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory org.sgkyros.common.dao.impl.GenericDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/application-context.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: org.sgkyros.common.entity.Sgordcompra.sgpersona in org.sgkyros.common.entity.Sgpersona.sgordcomprasForIcodComprador


但是,如果我注释与“ Sgpersona”类相关的代码行,请对其进行测试,这对我来说通常是正常的,应该是...吗?

private Set<Sgordcompra> sgordcomprasForIcodComprador = new HashSet<Sgordcompra>(0);
private Set<Sgordcompra> sgordcomprasForIcodSolicitante = new HashSet<Sgordcompra>(0);

最佳答案

mappingBy属性试图在类“ Sgordcompra”中引用无效的属性“ sgpersona”。 OneToMany映射应按以下方式进行:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "sgpersonaByIcodComprador")
public Set<Sgordcompra> getSgordcomprasForIcodComprador() {
    return this.sgordcomprasForIcodComprador;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "sgpersonaByIcodSolicitante")
public Set<Sgordcompra> getSgordcomprasForIcodSolicitante() {
    return this.sgordcomprasForIcodSolicitante;
}

07-24 12:50