MappedBy引用两次未知的目标实体属性?

时间:2015-01-23 04:42:23

标签: java hibernate

我有一个查询,我碰巧有一个名为“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);

2 个答案:

答案 0 :(得分:2)

mappedBy属性试图引用无效的属性&#34; sgpersona&#34;在班级&#34; Sgordcompra&#34;。 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;
}

答案 1 :(得分:0)

我认为您的问题出在 Sgrencajchica 课程中。你需要一个名为&#34; sgpersona&#34;的属性。在里面。发布 Sgrencajchica 类的代码。