Java Hibernate mappedBy=";表“U名称”;这是一个令人困惑的问题
我有两个域模型,如下所示Java Hibernate mappedBy=";表“U名称”;这是一个令人困惑的问题,java,hibernate,naming-conventions,Java,Hibernate,Naming Conventions,我有两个域模型,如下所示 @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
@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;
}
还有下面的能手和二传手 您没有将表名放入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类是这样的
public class Ctc {
//other properties
@OneToOne
@JoinColumn
private Candidate candidate
您没有将表名放在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类是这样的
public class Ctc {
//other properties
@OneToOne
@JoinColumn
private Candidate candidate
你是说我的实体的类名吗?@ManojShukla我编辑了答案,没有对你的类的引用,检查我给出的示例是的,在你编辑后,我添加了我的
Ctc.java
这正是你所说的。如果这回答了你的问题,请接受答案否你没有回答我的问题。。我的问题是关于mappedBy
的值,你明白了吗?你是指我的实体的类名吗?@ManojShukla我编辑了答案,没有对你的类的引用,检查我给出的示例是,在你编辑之后,我添加了我的Ctc.java
这正是你所说的。如果这回答了你的问题,请接受回答否你没有回答我的问题。。我的问题是关于mappedBy
的价值,你明白了吗??