views:

217

answers:

2

I'm learning NHibernate to learn a more robust ORM than linq to sql, but i'm struggling with some simple things.

My first challenge is mapping this ID column using hbm.xml.

CREATE TABLE [dbo].[Party](
   [Id] [int] IDENTITY(1,1) NOT NULL
 CONSTRAINT [PK_Party] PRIMARY KEY CLUSTERED 
(
[Id] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

When I use the following mapping, i get a weird exported schema DDL

   <class name="Party" table="Party">
     <id name="Id">
      <column name="Id" />
      <generator class="native" ></generator>
     </id>
   </class>

which generates the following using the export tool

create table Party (
    Id INT IDENTITY NOT NULL,
    primary key (Id)
)

using Nhibernate, how can I add the clustered primary key and auto-incrementing Id?

Or, is the way of the world in NHibernate to keep your db schema separate from the mapping and use generator=native?

I'm starting to think NHibernate shouldn't be used for schema generation if you've already got a db schema that's fine tuned to a particular server.

Any help is greatly appreciated.

+1  A: 

I do not understand. You already have your DB, so why would you want to use NHibernate to generate the schema of the DB ? I would just create my mappings for my classes so that they map to the existing DB schema ?

I don't know if it is possible to specify in your NHibernate mapping that the primary key should be on a clustered index, etc... since this is quite RDBMS-specific. The notion of clustered indexes is something that is quite particular to SQL Server I guess.

Frederik Gheysels
Thanks. I'll aim to just create a mapping without trying to be model first for all RDBMS things I need. how does nhib know that ID column is autogenerate by the DB?
CVertex
NHibernate knows because of what you specified in your class mapping.For example, if I specifiy:<generator class="increment" />Then it knows to make it an auto-incrementing identity field. More on generators here:http://barchitect.blogspot.com/2008/07/nhibernate-generator-and-primary-key.html
Cocowalla
+1  A: 

Assuming you're working with Microsoft SQL (look like it) the schema DDL that NHibernate is generating will create the primary key as a clustered index.

MSSQL creates primary keys as clustered index by default (actually, I think they are always clustered).

Cocowalla
Cocawalla, just so you know, yes a clustered index is the default for a primary key but it is not a requirement. You cna choose some other index to be the clustered index.
HLGEM