Aditya
Aditya

Reputation: 1043

Hibernate foreign key without reference to foreign key Entity

I use Hibernate 4.2. I have two tables say Employee and Employer. I have employer_id in employee table as foreign key.

Now in Employee.java file can I have

@Column(name="employer_id") 
private Integer employerId;

I don't want to have a Employer reference in Employee as I don't want to fetch Employer data every time I fetch employee.

Also please let me know is there a way I can have Employer reference and make Hibernate not to fetch it unless an request is made say getEmployer();

Upvotes: 1

Views: 824

Answers (1)

Ean V
Ean V

Reputation: 5429

You can do it using lazy loading put following annotation:

private Employer employer;
.
.
.

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="employer_id")
public Employer getEmployer() {
   return this.employer;
}

And employer will be loaded only when you call getEmployer()

Upvotes: 4

Related Questions