gpt4 book ai didi

java - org.hibernate.AnnotationException : Unknown mappedBy in: mdl. Complaint.jobDone,引用属性未知 : mdl. JobDone.jobDone

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:31:51 25 4
gpt4 key购买 nike

我尝试使用 hibernate 创建一对一映射并保存 SQL 数据库,但我运行我的项目时出现以下错误:

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

最佳答案

显然,问题是您在 JobDone 中没有属性 jobDone。你可以使用这个映射

public class Compilant {

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

}

您将在 Complaint 中有一个指向 JobDone 的外键。您可以从 JobDone 中删除 Compilant 属性。

如果你想在 JobDone 中也有一个 Complaint 的关联(这不是很方便)

public class JobDone {

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

}

如果你只想在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;
}

}

关于java - org.hibernate.AnnotationException : Unknown mappedBy in: mdl. Complaint.jobDone,引用属性未知 : mdl. JobDone.jobDone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35646042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com