views:

88

answers:

3

Let's say I have one class "User", and it "has" a property of type "Profile". How can I configure my mappings to generate the schema and create both tables in the database?

A: 

If you are asking about the mapping, you can use the join as shown below. note: you obviously have to add some additional attributes to suit your app.

   <?xml version="1.0" encoding="utf-8" ?>
   <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"    namespace="MyNamespace" assembly="MyAssembly"    default-lazy="true">  
   <class name="User" table="User">    
   <id name="Id" column="user_id" unsaved-value="0">      
   <generator class="native" />    
   </id>    
   <property name="Profile" column="profile" />  
   </class>
   </hibernate-mapping>

   <?xml version="1.0" encoding="utf-8" ?>
   <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"    namespace="MyNamespace" assembly="MyAssembly"    default-lazy="true">  
   <class name="Profile" table="Profile">    
   <id name="Id" column="profile_id" unsaved-value="0">      
   <generator class="native" />    
   </id>    
   <property name="Profile" column="profile" />    
   </class>
   </hibernate-mapping>
CodeToGlory
I couldn't get that to work, but thanks for the help.
mkelley33
+1  A: 
<many-to-one/>
Matt Hinze
Where would you put that tag and why? What if I had a home profile and business profile?
mkelley33
Nevermind my previous comment. That pointed me in the right direction. Thank you!
mkelley33
+1  A: 

As an aside, if you are not a great fan of scripting up hibernate mappings (which I'm not) then you have a couple of other options.

Castle ActiveRecord is one alternative - it's a layer on top of NHibernate which (among other things) lets you declare your relationships using attributes on your classes and properties.

And Fluent NHibernate is another - it lets you programmatically setup your classes and relationships.

Both are a great improvement over writing your mapping xml by hand!

Jonathan Moffatt
Thanks for the advice! I'll take a look at those.
mkelley33
I second this. Fluent NHibernate is the way to go.
CodeToGlory