Hi Guys,
Environment: Hibernate 2.0.3, MySQL
4.0.14b, Windows 2K.
have many-to-many,
many-to-one, and one-to-many working like a charm.
struggling with enforcing the same
key for a one-to-one.
example user.hbm.xml:
<hibernate-mapping>
<class name="example.User" table="users"> <id name="id" column="user_id" type="long" unsaved-value="null"><generator class="hilo"/></id> <property
name="name" column="user_name" type="string" length="15"
not-null="true"/>
<property name="email" column="email" type="string" length="30" /> <one-to-one name="address" class="example.Address" cascade="all"/>
</class>
</hibernate-mapping> example address.hbm.xml:
<hibernate-mapping>
<class name="example.Address" table="address"> <id name="id" column="address_id" type="long" unsaved-value="null"><generator class="hilo"/></id>
<property name="name" column="address_name"
type="string" length="15" not-null="true"/>
<property name="zip" column="zip" type="string" length="10" />
</class>
</hibernate-mapping> when I create an address object for a
specific user, it has a different id than the user's id: 2
trying to enforce with something like
addr.setId(user.getId()); does not work, still the address has an id of 65537
for example, so the delete of a user has no impact on the address.
Is something wrong in the hbm.xml? does somebody have a working example of the one-to-one relationship? Thanks, Mike
|