Hi,
I want to use one-to-one association on a primary key between some tables but when I've checked what kind of sgl is produce I've noticed that records from associated table are read during accessing main table.
Some details:
Hibernate version: 3.0
Mapping documents:
MAIN TABLE
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="test.Main" table="MAIN">
<id name="idmain" type="integer">
<column name="IDMAIN" precision="9" scale="0" />
<generator class="identity" />
</id>
<property name="col2" type="string">
<column name="COL2" length="50" />
</property>
<one-to-one name="Table2" cascade="all"/>
</class>
</hibernate-mapping>
ASSOCIATED TABLE
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="test.Table2" table="TABLE_2" schema="dbo" catalog="RAPORTY">
<id name="idmain" column="IDMAIN">
<generator class="foreign">
<param name="property">main</param>
</generator>
</id>
<one-to-one name="main" class="test.Main" constrained="true"/>
<property name="column2" type="string">
<column name="COLUMN_2" length="10" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Transaction tx = sesja.beginTransaction();
try {
obj = sesja.createQuery(coWybrac).list();
tx.commit();
}
catch(HibernateException hi1){
tx.rollback();
throw hi1;
}
Name and version of the database you are using: MS SQL
The generated SQL (show_sql=true):
Hibernate: select main0_.IDMAIN as IDMAIN, main0_.COL2 as COL2_1_ from RAPORTY.dbo.MAIN main0_
Hibernate: select table2x0_.IDMAIN as IDMAIN0_, table2x0_.COLUMN_2 as COLUMN2_0_0_ from RAPORTY.dbo.TABLE_2 table2x0_ where table2x0_.IDMAIN=?
Hibernate: select table2x0_.IDMAIN as IDMAIN0_, table2x0_.COLUMN_2 as COLUMN2_0_0_ from RAPORTY.dbo.TABLE_2 table2x0_ where table2x0_.IDMAIN=?
Hibernate: select table2x0_.IDMAIN as IDMAIN0_, table2x0_.COLUMN_2 as COLUMN2_0_0_ from RAPORTY.dbo.TABLE_2 table2x0_ where table2x0_.IDMAIN=?
Hibernate: select table2x0_.IDMAIN as IDMAIN0_, table2x0_.COLUMN_2 as COLUMN2_0_0_ from RAPORTY.dbo.TABLE_2 table2x0_ where table2x0_.IDMAIN=?
My relation between this table is exactly 1-0..1.
I have tried some lazy & fetch option but still have the same sqls. I don't want to use association on a foreign key because I want to add new associated table without changing on main table.
Is this situation normal for this kind of association or I should use some others options - if yes which one?
Thanks,
|