简体   繁体   中英

Why Table association is not adding left outer join in hibernate

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 ?

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 ie 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM