tags:

views:

63

answers:

2

Schema:

  • Users
    • ID
    • Name
    • Password
  • Address
    • ID
    • Street
    • UserID

Both tables have an ID field (guid).

Code:

User u = new User();
u.Address.Street = "test";
session.Save(u);

How can I create a mapping file to use the UserID from the address table to point to the ID from the Users table and reflect my sample code above?

A: 

In most of the cases, such kind of mapping should lead to a 'component'.
In your case, Address could be a component of User. This means that you'll have indeed an Adress class, and the User class will have a property of type Adress, but the Address is saved in the Users table in the DB.

If you really want a one-to-one mapping with the DB layout like you want, I suggest you create a one-to-one mapping in the User mapping, and a many-to-one mapping in the Address mapping (with unique set to true). But, I think that this also means that you will be responsible of deleting the old address first if you update the adress of a user ...

Frederik Gheysels
This is working to save my two objects:User.hbm.xml <class name="SCI4.Domain.User, SCI4" table="User"> <id name="Id" type="string"> <column name="Id" /> <generator class="uuid.hex" /> </id> <property name="Name"></property> <property name="Password"></property> <property name="Type"></property> <one-to-one name="Address" class="SCI4.Domain.Address" cascade="all"/> </class>
CrazyJoe
Address.hbm.xml: <class name="SCI4.Domain.Address, SCI4" table="Address"> <id name="Id" type="string"> <column name="Id" /> <generator class="uuid.hex" /> </id> <property name="ZipCode"></property> <property name="StreetType"></property> <property name="Street"></property> <property name="Number"></property> <property name="Neighborhood"></property> <property name="State"></property> <property name="City"></property> <property name="OwnerId"></property> </class>
CrazyJoe
this tag is my problem, howto i fill then with the user.id value???<property name="OwnerId">
CrazyJoe
A: 

Using many-to-one i sucessfully solve my problem.

but i go search more samples!!

thanks!!

CrazyJoe