Hibernate Version : 2.1
Database : Oracle 8.1.7
I have two classes Loan and Borrower, and they have a 1:1 relationship between them.
The Java files are as follows :
Code:
package tavant.platform.persistence.hibernate.simple.model;
public class SimpleLoan
{
private Long id;
private SimpleBorrower borrower;
public Long getId()
{
return id;
}
private void setId(Long id)
{
this.id = id;
}
public SimpleBorrower getBorrower()
{
return borrower;
}
public void setBorrower(SimpleBorrower borrower)
{
this.borrower = borrower;
}
}
AND
Code:
package tavant.platform.persistence.hibernate.simple.model;
public class SimpleBorrower
{
private Long id;
private SimpleLoan loan;
public Long getId()
{
return id;
}
private void setId(Long id)
{
this.id = id;
}
public SimpleLoan getLoan()
{
return loan;
}
public void setLoan(SimpleLoan loan)
{
this.loan = loan;
}
}
The corresponding mapping files are :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="tavant.platform.persistence.hibernate.simple.model">
<class name="SimpleLoan" table="LOAN" proxy="SimpleLoan">
<id name="id" column="LOAN_ID" type="long">
<generator class="sequence">
<param name="sequence">LOAN_SEQ</param>
</generator>
</id>
<many-to-one name="borrower" class="SimpleBorrower" column="PRIMARY_BORROWER_ID" unique="true"/>
</class>
</hibernate-mapping>
AND
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="tavant.platform.persistence.hibernate.simple.model">
<class name="SimpleBorrower" table="LOAN_BORROWER" proxy="SimpleBorrower">
<id name="id" column="LOAN_BORROWER_ID" type="long">
<generator class="sequence">
<param name="sequence">LOAN_BORROWER_SEQ</param>
</generator>
</id>
<one-to-one name="loan" class="SimpleLoan" constrained="true" outer-join="false" property-ref="borrower"/>
<!-- many-to-one name="spouse" class="SimpleBorrower" column="SPOUSE_LOAN_BORROWER_ID" unique="true"/ -->
</class>
</hibernate-mapping>
I want eager loading of the loan-borrower relationship. I use the following the test code :
Code:
private static void test1() throws HibernateException
{
Session session = openSession();
SimpleLoan loan = (SimpleLoan) session.createQuery("from SimpleLoan l " +
"join fetch l.borrower b " +
"where l.id=" + 8997609029589973L)
.uniqueResult();
System.out.println("Borrower is " + loan.getBorrower().getId());
session.close();
}
private static Session openSession() throws HibernateException
{
Configuration cfg = new Configuration();
cfg.addClass(SimpleBorrower.class);
cfg.addClass(SimpleLoan.class);
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
return session;
}
The SQL Hibernate generates is :
Code:
select simpleloan0_.LOAN_ID as LOAN_ID0_, simpleborr1_.LOAN_BORROWER_ID as LOAN_BOR1_1_, simpleloan0_.PRIMARY_BORROWER_ID as PRIMARY_2_0_ from LOAN simpleloan0_, LOAN_BORROWER simpleborr1_ where simpleloan0_.PRIMARY_BORROWER_ID=simpleborr1_.LOAN_BORROWER_ID and ((simpleloan0_.LOAN_ID=8997609029589973 ))
select simpleloan0_.LOAN_ID as LOAN_ID0_, simpleloan0_.PRIMARY_BORROWER_ID as PRIMARY_2_0_ from LOAN simpleloan0_ where simpleloan0_.PRIMARY_BORROWER_ID=?
Hibernate first JOINs the Loan and Borrower entities, but then it fires a second query to load Loan entity again. How can I avoid the second query?
Thanks,
Binil