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]