Issue
i have a table and it associated to another table as one to one . In my service class i am calling findById(id).
@Entity
@Table(name = "CRL_EC")
public class LoanOrder {
@OneToOne(fetch = FetchType.EAGER ,cascade = CascadeType.ALL)
@JoinColumn(name = "loan_id" , referencedColumnName = "fLoanId" ,insertable = false ,updatable = false)
LoanEc loanEc;
}
@Entity
@Table(name = "LOAN_EC")
public class LoanEc {
@Id
Long fLoanId;
@OneToOne
@JoinColumn(name = "fLoanId",referencedColumnName = "loan_id" )
LoanOrder loanOrder;
}
public interface ECRepository extends Repository<LoanOrder,Long>{
void save(LoanOrder loanOrder);
}
When I am calling findById(id)
through my ECRepository hibernate calling it as separate queries.
In console I see the queries as
select * from LoanOrder where loan_id = ?
select * from LoanEc where fLoanId = ?
and the result is only if the id existss in second table(LoanEc). My expectation is
select * from LoanOrder left outer join LoanEc on loan_id = ?
Why its not associating these two entities ?
Solution
Hibernate doesn't do a join because there is a cycle in your entity graph and before Hibernate 6.0, the cycle is stopped as early as possible. In 6.0 we changed that to stop at the later i.e. you would get the left outer join.
I don't know what you mean by "associating these two entities" or "the result is only if the id exists", but if LoanEc
is the only attribute in LoanOrder
, or part of the id, then the entity won't be materialized if no LoanEc
exists for a LoanOrder
.
Answered By - Christian Beikov