Hi,
I've here a problem with one-to-one mapping.
I've tow classes Instrument and Option. The Relation between them is
Code:
+------------+ +----------+
| Instrument | | Option |
+------------+ 1 0..1 +----------+
| id* |<>--------| id* |
| ... | | inst_id |
+------------+ +----------+
Each classe has its own table defined as follows:
Code:
create table instrument (
id int not null auto_increment primary key,
name varchar(30) not null
);
create table option (
id int not null auto_increment primary key,
inst_id int not null,
constraint instrument_option_fk_01 foreign key (inst_id) references instrument (id)
);
The corresponig hbm file contains the definition of the classes and the one-to-one relation as follows:
Code:
<hibernate-mapping package="xxx" default-lazy="false">
<class name="Instrument" table="instrument" lazy="false">
<id name="id"
column="id"
type="int"
unsaved-value="0"
>
<generator class="native"/>
</id>
<property
name="Name"
column="name"
length="30"
type="string"
not-null="true"
/>
<one-to-one
name="option"
class="Option"
fetch="join"
lazy="false"
/>
</class>
<class name="Option" table="option" lazy="false">
<id name="id"
column="id"
type="int"
unsaved-value="0"
>
<generator class="native"/>
</id>
</class>
</hibernate-mapping>
The call of my dao for loading all Instruments (my DAO is extending
HibernateDaoSupport) is implemented as follows:
Code:
public List<Instrument> getAll() {
return getHibernateTemplate().loadAll(Instrument.class);
}
In my data base I've three Instruments and two Options. The instrument with the id 1 is not referenced by any option. The instruments with id 2 and 3 are referenced by options with id 1 and 2.
After calling loadAll() I get all three Instruments but the option objects are not attached correctly. The first Instrument (with id 1) contains the first option and that with id 2 the another option. The third instrument doen't have any options.
It seems that the hibernate loads all instruments and all options and then starts to attach the options in the order of instruments. It doesn't take care of given inst_id.
What do I wrong.
I'm using hibernate 3.1 b2 with JDK 1.5. For tests I'm using MsSql 4.1
Thanks in advance,
Reza