-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 
Author Message
 Post subject: Eager Loading of 1:1 association
PostPosted: Tue Jul 20, 2004 2:22 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 7:23 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Use JOIN FETCH or Criteria.setFetchMode("association.name", FetchMode.EAGER);
http://forum.hibernate.org/viewtopic.php?p=2208765

--
Leonid


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 7:24 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
shl wrote:
Use JOIN FETCH or Criteria.setFetchMode("association.name", FetchMode.EAGER);
http://forum.hibernate.org/viewtopic.php?p=2208765


Pardon, I am not write.


Top
 Profile  
 
 Post subject: Re: Eager Loading of 1:1 association
PostPosted: Tue Jul 20, 2004 7:31 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
binil wrote:
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?


Are you sure that both SQL queries belong to one HQL query?
Enable timestamp for the log category.


Top
 Profile  
 
 Post subject: Re: Eager Loading of 1:1 association
PostPosted: Tue Jul 20, 2004 10:03 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
shl wrote:
Are you sure that both SQL queries belong to one HQL query?

Yes I am, because I have only one HQL query.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 10:05 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
please check hibernate version.
have you tried outer join = true?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 20, 2004 10:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is a known problem for nullable <one-to-one property-ref/>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 12:53 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
gavin wrote:
This is a known problem for nullable <one-to-one property-ref/>


I tried to find a bug report for this in JIRA, but couldnt. If someone here knows where the report is, please post a link.

Thanks,
Binil


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 12:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It is on the TODO list, which is permissioned.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 03, 2004 7:47 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
For those who are interested. the Hibernate 3 changelog here says fixed performance problems with <one-to-one property-ref=.../>.

I ran the example above using Hibernate3, but it still fires two queries :(

_________________
Thanks,
Binil Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 03, 2004 7:48 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
binil wrote:
I ran the example above using Hibernate3, but it still fires two queries :(


I meant Hibernate3 alpha.

_________________
Thanks,
Binil Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 03, 2004 1:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I don't believe you. But if you insist, submit an ultra-simple, runnable test case to JIRA.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 04, 2004 6:45 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
Hi Gavin,

You are right - it is fixed! My appologies for the false alarm.

I was thinking that I was using Hibernate3alpha, when actually I was using Hibernate2. I rechecked everything again today, and yes it works.

Thanks a mil!

_________________
Thanks,
Binil Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 04, 2004 6:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
:-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.