miiiii
miiiii

Reputation: 1610

Hibernate mappedBy="table_name" is confusing one

I have two domain models as follows,

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

}

When I try to run the application, it gives me this exception.

org.hibernate.AnnotationException: Unknown mappedBy in: com.hrsystem.model.Candidate.ctc, referenced property unknown: com.hrsystem.model.Ctc.Candidate

But if I put the value of mappedBy exactly as database-convention (i.e. lower case letters as in @Table(name="candidate") ), it works perfectly fine.

So my question is, why we should encourage database-convention driven development though we are using Object Oriented Design?

UPDATE---

Here is my Ctc.java entity.

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

}

and getters and setters below it..!!

Upvotes: 0

Views: 526

Answers (1)

Amer Qarabsa
Amer Qarabsa

Reputation: 6574

you do not put the table name in the mappedBy, you put the name of the reference to your object.

So in your case

@OneToOne(mappedBy="candidate", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
//above here, mappedBy has Class name to map, as OO Design suggests.

private Ctc ctc;

We would expect Ctc class to be something like that

public class Ctc {

//other properties
@OneToOne
@JoinColumn
 private Candidate candidate

Upvotes: 2

Related Questions