使用同一个表中的两个外键时抛出bean创建异常

时间:2014-10-20 11:52:06

标签: java sql-server spring foreign-key-relationship

我在我的项目中使用mssql server和hibernate3。我得到了以下异常

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Invocation of init method failed; 
nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.uniphore.timesheet.domain.TblBaseLine.tblUserDetails in com.uniphore.timesheet.domain.TblUserDetails.tblBaseLinesForApproverId

我在同一张桌子上使用了两个foriegn键。但我不知道如何为此编写注释。

例如,考虑以下模型

User    
userid(PK)
role 
name

Assignment
AssignmentID(pk)
studentID(Fk_User)
professiorID(Fk_user)

以下是我的课程: TblBaseline

package com.uniphore.timesheet.domain;



import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="tblBaseLine")
public class TblBaseLine  implements java.io.Serializable {


    private static final long serialVersionUID = -1798070786993154671L;
     @Id
     @GeneratedValue
     @Column(name ="ID")
     private int id;
     @ManyToOne
     @JoinColumn(name="ModifierID")
     private TblUserDetails tblUserDetailsByModifierId;
     @ManyToOne
     @JoinColumn(name="ApproverID")
     private TblUserDetails tblUserDetailsByApproverId;

     @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblBaseLine", cascade = CascadeType.ALL)
     private Set<TblPhaseBaseLine> tblPhaseBaseLines = new HashSet<TblPhaseBaseLine>(0);

    public TblBaseLine() {
    }


    public TblBaseLine(int id) {
        this.id = id;
    }


}

TblPhaseBaseLine

package com.uniphore.timesheet.domain;



import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="tblPhaseBaseLine")
public class TblPhaseBaseLine  implements java.io.Serializable {

    private static final long serialVersionUID = -1798070786993154674L;
     @Id
     @GeneratedValue
     @Column(name ="ID")
     private int id;
     @ManyToOne
     @JoinColumn(name ="PhaseID")
     private TblProjectPhase tblProjectPhase;
     @ManyToOne
     @JoinColumn(name ="BaseLineID")
     private TblBaseLine tblBaseLine;

     @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblPhaseBaseLine", cascade = CascadeType.ALL)
     private Set<TblProjectPhase> tblProjectPhases = new HashSet<TblProjectPhase>(0);

    public TblPhaseBaseLine() {
    }


    public TblPhaseBaseLine(int id) {
        this.id = id;
    }




}

TblUserDetails:

package com.uniphore.timesheet.domain;



import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="tblUserDetails")
public class TblUserDetails  implements java.io.Serializable {
    private static final long serialVersionUID = -1798070786993154686L;
     @Id
     @GeneratedValue
     @Column(name ="ID")
     private int id;
     @ManyToOne
     @JoinColumn(name ="RoleID")
     private TblRoles tblRoles;


     @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblUserDetails", cascade = CascadeType.ALL)
     private Set<TblUserPrevilege> tblUserPrevileges = new HashSet<TblUserPrevilege>(0);
     @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblUserDetails", cascade = CascadeType.ALL)
     private Set<TblProjectApprover> tblProjectApprovers = new HashSet<TblProjectApprover>(0);
     @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblUserDetails", cascade = CascadeType.ALL)
     private Set<TblProjectResource> tblProjectResources = new HashSet<TblProjectResource>(0);
     @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblUserDetails", cascade = CascadeType.ALL)
     private Set<TblTimesheet> tblTimesheetsForApprover = new HashSet<TblTimesheet>(0);
     @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblUserDetails", cascade = CascadeType.ALL)
     private Set<TblBaseLine> tblBaseLinesForModifierId = new HashSet<TblBaseLine>(0);
     @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblUserDetails", cascade = CascadeType.ALL)
     private Set<TblTimesheet> tblTimesheetsForUserId = new HashSet<TblTimesheet>(0);
     @OneToMany(fetch = FetchType.LAZY, mappedBy = "tblUserDetails", cascade = CascadeType.ALL)
     private Set<TblBaseLine> tblBaseLinesForApproverId = new HashSet<TblBaseLine>(0);

    public TblUserDetails() {
    }




}

如何避免此异常? 任何帮助将不胜感激!!!

1 个答案:

答案 0 :(得分:1)

你没有避免异常 - 你解决了这个问题。该错误清楚地告诉您com.uniphore.timesheet.domain.TblBaseLine没有实体属性&#39; tblUserDetails&#39;。事实上,它并没有。

所以&#39; mappedBy&#39;你有TblUserDetails是完全错误的,使用正确的匹配类属性名称。 tblUserDetailsByModifierId或tblUserDetailsByApproverId是您可以在&#39; mappedBy&#39;中使用的可能属性名称。