Hibernate mappedBy="table_name" 令人困惑
Hibernate mappedBy="table_name" is confusing one
我有如下两个领域模型,
@Entity
@Table(name = "candidate") // lowercase-for-database-conventions
public class Candidate {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
.
.
.
@OneToOne(mappedBy="Candidate", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
//above here, mappedBy has Class name to map, as OO Design suggests.
private Ctc ctc;
}
当我尝试 运行 应用程序时,它给了我这个异常。
org.hibernate.AnnotationException: Unknown mappedBy in: com.hrsystem.model.Candidate.ctc, referenced property unknown: com.hrsystem.model.Ctc.Candidate
但是,如果我将 mappedBy
的值完全按照数据库约定(即 @Table(name="candidate")
中的小写字母),它就可以正常工作。
所以我的问题是,尽管我们使用面向对象设计,但为什么我们应该鼓励数据库约定驱动的开发?
更新---
这是我的 Ctc.java
实体。
@Entity
@Table(name = "ctc")
public class Ctc {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private double basic;
private double hra;
private double da;
@OneToOne
@JoinColumn(name = "candidate_id")
private Candidate candidate;
}
以及下面的 getter 和 setter..!!
您没有将 table 名称放在 mappedBy 中,而是将引用的名称放在您的对象中。
所以在你的情况下
@OneToOne(mappedBy="candidate", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
//above here, mappedBy has Class name to map, as OO Design suggests.
private Ctc ctc;
我们希望 Ctc class 是那样的东西
public class Ctc {
//other properties
@OneToOne
@JoinColumn
private Candidate candidate
我有如下两个领域模型,
@Entity
@Table(name = "candidate") // lowercase-for-database-conventions
public class Candidate {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
.
.
.
@OneToOne(mappedBy="Candidate", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
//above here, mappedBy has Class name to map, as OO Design suggests.
private Ctc ctc;
}
当我尝试 运行 应用程序时,它给了我这个异常。
org.hibernate.AnnotationException: Unknown mappedBy in: com.hrsystem.model.Candidate.ctc, referenced property unknown: com.hrsystem.model.Ctc.Candidate
但是,如果我将 mappedBy
的值完全按照数据库约定(即 @Table(name="candidate")
中的小写字母),它就可以正常工作。
所以我的问题是,尽管我们使用面向对象设计,但为什么我们应该鼓励数据库约定驱动的开发?
更新---
这是我的 Ctc.java
实体。
@Entity
@Table(name = "ctc")
public class Ctc {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private double basic;
private double hra;
private double da;
@OneToOne
@JoinColumn(name = "candidate_id")
private Candidate candidate;
}
以及下面的 getter 和 setter..!!
您没有将 table 名称放在 mappedBy 中,而是将引用的名称放在您的对象中。
所以在你的情况下
@OneToOne(mappedBy="candidate", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
//above here, mappedBy has Class name to map, as OO Design suggests.
private Ctc ctc;
我们希望 Ctc class 是那样的东西
public class Ctc {
//other properties
@OneToOne
@JoinColumn
private Candidate candidate