CVertex
CVertex

Reputation: 18247

Mapping my existing ID column using NHibernate that keeps identity incrementing

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.

Upvotes: 0

Views: 1228

Answers (2)

Cocowalla
Cocowalla

Reputation: 14350

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).

Upvotes: 1

Frederik Gheysels
Frederik Gheysels

Reputation: 56984

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.

Upvotes: 1

Related Questions