Hi,
Please someone look into this and please tell me what I am
doing wrong here.
I tried one-to-one in 2 ways:
In two cases, I wrote the mapping file then I generated java
classes and
DB schema for MySQL. Then wrote the usecase. And here I am
giving the results
I got when I executed the usecase.
I CASE
-----------------------------------------------------------------------------------------------------------------------
Mapping
-------------
<hibernate-mapping>
<import class="onetoone.Recurrence" rename="recurrence"/> <class name="onetoone.Recurrence" table="recurrence"> <id name="recurrenceId" column="recurrence_id" unsaved-value="0" type="long"> <generator class="identity"/> </id> <one-to-one name="pattern" class="onetoone.Pattern" cascade="all"/> <property name="name" column="name" type="string"/> </class> <class name="onetoone.Pattern"
table="pattern">
<id name="recurrenceId" column="recurrence_id" unsaved-value="0" type="long"> <generator class="assigned"/> </id> <property name="recurrenceCount" column="recurrence_count" type="long" /> </class> </hibernate-mapping> Usecase
------------
Recurrence rec = new
Recurrence();
Pattern pattern = new Pattern(); pattern.setRecurrenceCount(23); rec.setName("test"); rec.setPattern(pattern); session.save(rec); Results
----------
recurrence record getting saved. But no details are getting
saved in pattern.
The generated SQL queries:
insert into recurrence (name) values (?) II CASE
-----------------------------------------------------------------------------------------------------
Here I made the relation bi-directional.
Class Mapping
---------------------
<hibernate-mapping>
<class name="onetoone2.Recurrence" table="recurrence"> <id name="recurrenceId" column="recurrence_id" unsaved-value="0" type="long"> <generator class="identity"/> </id> <one-to-one name="pattern" class="onetoone2.Pattern" cascade="all"/> <property name="name" column="name" type="string"/> </class> <class name="onetoone2.Pattern"
table="pattern">
<id name="recurrenceId" column="recurrence_id" unsaved-value="0" type="long"> <generator class="assigned"/> </id> <property name="recurrenceCount" column="recurrence_count" type="long" /> <one-to-one name="recurrence" class="onetoone2.Recurrence" constrained="true"/> </class> </hibernate-mapping> Usecase
-------------
Recurrence rec = new
Recurrence();
Pattern pattern = new Pattern(); pattern.setRecurrenceCount(23); rec.setName("test"); pattern.setRecurrence(rec); rec.setPattern(pattern); session.save(rec); Results
----------
recurrence record is getting saved. and pattern record is also getting saved. but recurrence_id is not getting saved in the pattern
table.
insert into recurrence (name) values (?)
Can anyone please tell me if I am doing anything
wrong. What is the correct way
to map one-to-one relation. How can I save the
identity in the second-level entity?.
Thanks in advance.
dosapati
|