2016-02-26 31 views
5

我試着創建一對一映射並使用hibernate保存SQL數據庫,但是我運行我的項目I我收到以下錯誤:org.hibernate.AnnotationException:未知mappedBy在:mdl.Complaint.jobDone,引用屬性未知:mdl.JobDone.jobDone

Initial SessionFactory creation failed.org.hibernate.AnnotationException: Unknown mappedBy in: mdl.Complaint.jobDone, referenced property unknown: mdl.JobDone.jobDone 
Exception in thread "main" java.lang.ExceptionInInitializerError 
    at config.HibernateUtil.buildSessionFactory(HibernateUtil.java:19) 
    at config.HibernateUtil.<clinit>(HibernateUtil.java:8) 
    at application.JD_Comp.main(JD_Comp.java:22) 
Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: mdl.Complaint.jobDone, referenced property unknown: mdl.JobDone.jobDone 
    at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:159) 
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1689) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1396) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1829) 
    at config.HibernateUtil.buildSessionFactory(HibernateUtil.java:13) 
    ... 2 more 

還有就是我的映射類:

@Entity 
@Table(name = "Complaint", catalog = "jobcard") 
public class Complaint implements Serializable{ 
    private Integer complaint_id; 
    private String nature; 
    private String instruction; 
    private JobDone jobDone; 

    /** 
    * @return the complaint_id 
    */ 
    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(name = "complaint_id", unique = true, nullable = false) 
    public Integer getComplaint_id() { 
     return complaint_id; 
    } 

    /** 
    * @param complaint_id the complaint_id to set 
    */ 
    public void setComplaint_id(Integer complaint_id) { 
     this.complaint_id = complaint_id; 
    } 

    /** 
    * @return the nature 
    */ 
    public String getNature() { 
     return nature; 
    } 

    /** 
    * @param nature the nature to set 
    */ 
    public void setNature(String nature) { 
     this.nature = nature; 
    } 

    /** 
    * @return the instruction 
    */ 
    public String getInstruction() { 
     return instruction; 
    } 

    /** 
    * @param instruction the instruction to set 
    */ 
    public void setInstruction(String instruction) { 
     this.instruction = instruction; 
    } 

    /** 
    * @return the jobDone 
    */ 
    @OneToOne(fetch = FetchType.LAZY, mappedBy = "jobDone", cascade = CascadeType.ALL) 
    public JobDone getJobDone() { 
     return jobDone; 
    } 

    /** 
    * @param jobDone the jobDone to set 
    */ 
    public void setJobDone(JobDone jobDone) { 
     this.jobDone = jobDone; 
    } 

} 

其他類:

@Entity 
@Table(name = "JobDone", catalog = "jobcard", uniqueConstraints = { 
     @UniqueConstraint(columnNames = "nature"), 
     @UniqueConstraint(columnNames = "amount") }) 
public class JobDone implements Serializable{ 
    private Integer jobDone_id; 
    private String nature; 
    private Integer amount; 
    private Complaint complaint; 

    /** 
    * @return the jobDone_id 
    */ 
    @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "Complaint")) 
    @Id 
    @GeneratedValue(generator = "generator") 
    @Column(name = "jobDone_id", unique = true, nullable = false) 
    public Integer getJobDone_id() { 
     return jobDone_id; 
    } 

    /** 
    * @param jobDone_id the jobDone_id to set 
    */ 
    public void setJobDone_id(Integer jobDone_id) { 
     this.jobDone_id = jobDone_id; 
    } 

    /** 
    * @return the nature 
    */ 
    public String getNature() { 
     return nature; 
    } 

    /** 
    * @param nature the nature to set 
    */ 
    public void setNature(String nature) { 
     this.nature = nature; 
    } 

    /** 
    * @return the amount 
    */ 
    public Integer getAmount() { 
     return amount; 
    } 

    /** 
    * @param amount the amount to set 
    */ 
    public void setAmount(Integer amount) { 
     this.amount = amount; 
    } 

    /** 
    * @return the complaint 
    */ 
    @OneToOne(fetch = FetchType.LAZY) 
    @PrimaryKeyJoinColumn 
    public Complaint getComplaint() { 
     return complaint; 
    } 

    /** 
    * @param complaint the complaint to set 
    */ 
    public void setComplaint(Complaint complaint) { 
     this.complaint = complaint; 
    } 
} 

SQL查詢:

CREATE DATABASE jobcard; 
USE jobcard; 
CREATE TABLE Complaint(
    jobNumber varchar(100), 
    complaint_id int(10) primary key NOT NULL AUTO_INCREMENT, 
    nature varchar(200), 
    instruction varchar(200), 
    constraint foreign key(jobNumber) references JobCard(jobNumber) 
    on delete cascade on update cascade 
); 

CREATE TABLE JobDone(
    complaint_id int(10), 
    jobDone_id int(10) primary key NOT NULL AUTO_INCREMENT, 
    nature varchar(200), 
    amount int, 
    constraint foreign key(complaint_id) references Complaint(complaint_id) 
    on delete cascade on update cascade  
); 

我創建hibernate.cfg.xml文件和我映射完全合格的類名這兩個類。爲什麼我得到這個錯誤。

更新

這是工作

投訴類:

@OneToOne(fetch = FetchType.LAZY) 
@PrimaryKeyJoinColumn 
public JobDone getJobDone() { 
    return jobDone; 
} 

JobDone類:

@OneToOne(fetch = FetchType.LAZY, mappedBy = "jobDone", cascade = CascadeType.ALL) 
public Complaint getComplaint() { 
    return complaint; 
} 
+0

嘗試發佈配置xml文件 – Abdelhak

回答

0

的問題,很明顯,是你沒有財產jobDoneJobDone。您可以使用此映射

public class Compilant { 

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
    public JobDone getJobDone() { 
     return jobDone; 
    } 

} 

你將不得不在ComplaintJobDone的外鍵。您可以從JobDone刪除Compilant財產。

如果你想如果你想有一個外鍵只在JobDone

public class Compilant { 

    @OneToOne(fetch = FetchType.LAZY, mappedBy = "complaint" cascade = CascadeType.ALL) 
    public JobDone getJobDone() { 
     return jobDone; 
    } 

} 

public class JobDone { 

    @OneToOne(fetch = FetchType.LAZY) 
    public Complaint getComplaint() { 
     return complaint; 
    } 

} 
+0

@AsankaDJayasinghe更改爲什麼? –

+0

@AsankaDJayasinghe不客氣 –

0
有在 JobDoneComplaint太協會(這是不是很方便)

public class JobDone { 

    @OneToOne(fetch = FetchType.LAZY) 
    public Complaint getComplaint() { 
     return complaint; 
    } 

}  

這是因爲您在其他地方(大寫J)使用jobDone(小j)和JobDone。糾正並解決問題。

+0

這是一個非常奇怪的假設:) –

相關問題