-->
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.  [ 3 posts ] 
Author Message
 Post subject: Too much sql in a bidirectional one-to-one relation
PostPosted: Thu Nov 10, 2005 4:24 am 
Newbie

Joined: Wed Oct 26, 2005 7:35 am
Posts: 10
Hibernate version:
3.1

Mapping documents:

Code:
<hibernate-mapping package="org.hibernate.test.propertyref">

   <class name="Person">

     <id name="id">
       <generator class="hilo"/>
     </id>

     <property name="name" length="100"/>

     <one-to-one name="address"
       property-ref="person"/>

   </class>
   <class name="Address">

     <id name="id">
       <generator class="hilo"/>
     </id>

     <property name="address" length="300"/>

     <many-to-one name="person" unique="true"/>

   </class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:

import org.hibernate.test.TestCase

public class PropertyRefTest
        extends TestCase
{
   public PropertyRefTest(String str)
   {
     super(str);
   }

   public void testOneToOnePropertyRef()
   {
     Session     s = openSession();
     Transaction t = s.beginTransaction();

     Person      p = new Person();
     p.setName("Steve");

     Address a = new Address();
     a.setAddress("Texas");

     p.setAddress(a);
     a.setPerson(p);
     s.save(p);
     s.save(a);

     s.flush();
     s.clear();

     a = (Address) s.get(
         Address.class,
         a.getId());  //get address

     assertEquals( " Hibernate.isInitialized( a.getPerson() )",
       false, Hibernate.isInitialized( a.getPerson()));

     System.out.println(" before initialize");
     System.out.println(" initialize " + a.getPerson().getName());
     System.out.println(" after initialize");

     s.clear();

     t.commit();
     s.close();
  }

   protected String[] getMappings()
   {
     return new String[] { "propertyref/Person.hbm.xml" };
   }

   public static Test suite()
   {
     return new TestSuite(PropertyRefTest.class);
   }

   protected void configure(Configuration cfg)
   {
     cfg.setProperty(Environment.DEFAULT_BATCH_FETCH_SIZE, "1");
     cfg.setProperty(Environment.GENERATE_STATISTICS, "true");
   }
}



Name and version of the database you are using:
The hsql database which is shipped with hibernate 3.1 source code.
I use org.hibernate.test.TestCase to illustrate this problem

The generated SQL (show_sql=true):

Code:
Testsuite: org.hibernate.test.propertyref.PropertyRefTest
Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0,766 sec
------------- Standard Output ---------------
Hibernate:
    insert
    into
        Person
        (name, id)
    values
        (?, ?)
Hibernate:
    insert
    into
        Address
        (address, person, id)
    values
        (?, ?, ?)
Hibernate:
    select
        address0_.id as id11_0_,
        address0_.address as address11_0_,
        address0_.person as person11_0_
    from
        Address address0_
    where
        address0_.id=?
before initialize
Hibernate:
    select
        person0_.id as id10_1_,
        person0_.name as name10_1_,
        address1_.id as id11_0_,
        address1_.address as address11_0_,
        address1_.person as person11_0_
    from
        Person person0_
    left outer join
        Address address1_
            on person0_.id=address1_.person
    where
        person0_.id=?
Hibernate:
    select
        address0_.id as id11_0_,
        address0_.address as address11_0_,
        address0_.person as person11_0_
    from
        Address address0_
    where
        address0_.person=?
initialize Steve
after initialize
------------- ---------------- ---------------


My problem:

I have an bidirectional one-to-one relation between Person and Address.
I get the Address from the database with Session.get(..).
Then I have an uninitialized assocation to Person.
When I call address.getPerson().getName(), I see an left outer join query to Person. During the same call I also see a query on Address. I don't expected that.
Why does it an query to Address, while it is already in the cache of the Session ?

Expand the problem with Batch fetching
I also have the problem with batch-fetching.
I change my mapping in address:
<class name="Person" batch-size="4">

When I get multiple Addresses in a query and then retrieve for each Address its Person, batch-fetching is used.
So during my first batch retrieval I will get four Person-record in one query. But I also get four seperated queries on Address, that's not what I expect.
Why does it queries to Address while it is already in its session cache ?



[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 11:55 am 
Newbie

Joined: Wed Oct 26, 2005 7:35 am
Posts: 10
I think the problem is that hibernate can not search Entities by foreign key in the cache.

So this is a can feature be a feature in future.

Here in org.hibernate.type.EntityType you can see the switch.
So if it is not a reference to a primairy key hibernate goe to the database.
Code:
       if ( isNull(owner, session) ) return null; //EARLY EXIT!

       if ( isReferenceToPrimaryKey() ) {
         return resolveIdentifier( (Serializable) value, session );
       }
       else {
         return loadByUniqueKey(
             getAssociatedEntityName(),
             uniqueKeyPropertyName,
             value,
             session
           );
       }


Is this right ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 19, 2005 6:16 am 
Newbie

Joined: Fri Jul 15, 2005 7:22 am
Posts: 3
If you add constraint="true" to your one-to-one statement hibernate should stop selecting the address when you get the person.

There is an example in the hibernate documentation.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.