I have @OneToOne
relationship between two classes with the following structure:
ClassA is PendingActionJobs:
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Pending_Actions_Detail_Fk")
public PendingActionDetails getActionDetailsFk() {
return actionDetailsFk;
}
public void setActionDetailsFk(PendingActionDetails aActionDetailsFk) {
actionDetailsFk = aActionDetailsFk;
}
ClassB is called PendingActionDetails:
@OneToOne(mappedBy = "actionDetailsFk", fetch = FetchType.LAZY)
public PendingActionJobs getPendingJob() {
return pendingJob;
}
public void setPendingJob(PendingActionJobs aPendingJob) {
pendingJob = aPendingJob;
}
As a result the pending actions job table has an FK to the primary key in the PendingActionsDetail table. The PendingActionJobs table has many repeating Fks which map to some pk in the details table like:
Pk FK to the PendingActionsDetail
2 1
3 1
4 1
5 1
6 1
7 1
The PendingActionsDetail has only one primary key like:
Pk
1
when I call
createQuery("from PendingActionDetails details LEFT JOIN FETCH details.pendingJob")
it retrieves the collection but when I attempt to fetch the data from the other side, namely from the FK table it says
createQuery("from PendingActionJobs jobs LEFT JOIN FETCH jobs.actionDetailsFk")
it says - more than one row with the same identifier found for class PendingActionJobs. The equivalent sql statement should be:
select * from Pending_Actions_Job jobs
left join
Pending_Actions_Details details
on jobs.Pending_Actions_Detail_Fk =
details.Pending_Actions_Details_Primary_Key
But why does hibernate throw the error? All I want to populate each of the PendingActionJobs FK's rows with the data from the PendingActionDetails Pk. Why does it complain? In other words, I cannot fetch data from both sides but I have to use PendingActionsDetails class?
According to the scenario which you explained, You need OneToMany mapping instead of OneToOne. Go through the definition of both the mappings.
OneToOne Mapping
A OneToOne
relationship in Java is where the source object has an attribute that references another target object and (if) that target object had the inverse relationship back to the source object it would also be a OneToOne
relationship.
OneToMany Mapping
A OneToMany
relationship in Java is where the source object has an attribute that stores a collection of target objects and if those target objects had the inverse relationship back to the source object it would be a ManyToOne relationship.
In order to solve this issue, update your PendingActionDetails class to store List of PendingActionDetails as given below or make sure exactly one PendingActionJobs
exist for every PendingActionDetails
.
@OneToMany
List<PendingActionJobs>