Question

I need to create a non-clustered index with INCLUDE columns (see the <create> tag below). Here's the mapping file:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="MyApp" assembly="MyApp">
  <class name="User" table="user" >
    <id name="Id" type="Guid" column="user_id">
      <generator class="guid.comb"/>
    </id>

    <property name="Name" column="name" not-null="true"  />
    <property name="Phone" column="phone" />
    <property name="Zipcode" column="zipcode" />
  </class>

  <database-object>
    <create>
      CREATE NONCLUSTERED INDEX [IX_user_zipcode_id]
      ON User (Zipcode)
      INCLUDE (Name, Phone)
    </create>
    <drop>
      DROP INDEX IX_user_zipcode_id
    </drop>
    <dialect-scope name="NHibernate.Dialect.MsSql2000Dialect"/>
    <dialect-scope name="NHibernate.Dialect.MsSql2005Dialect"/>
    <dialect-scope name="NHibernate.Dialect.MsSql2008Dialect"/>

  </database-object>

</hibernate-mapping>

The problem I'm having is the index is not created at all. Nothing appears to be happening. This is my first time using <database-object> so I may be doing something wrong here.

I'm guessing INCLUDE is Sql Server specific which is why the dialect-scope is there. I know how to create a single and multi-column index, but this is not what I want. I want a single column index on zipcode and all other columns in the User table part of the INCLUDE clause of the query. Is there any way to create this type of index using the mapping file or some other way?

This is probably a long shot, but it would be nice to not have to specify every column but the indexed one in the INCLUDE part of the query... Instead to just let nhibernate add any new columns to the index that are added as properties to the mapping file.

Was it helpful?

Solution

So part of the problem was indeed my lack of understanding the database-object tag due mostly to poor documentation. From what I've gathered, the <create> and <drop> tags are only used when using SchemaExport like so:

    Dim schemaExport As SchemaExport = New SchemaExport(NhibernateConfiguration)
    schemaExport.Execute(False, True, False)

My app doesn't create the schema using that class. Instead it uses SchemaUpdate so the schema isn't blown away every time (the database may already exist on the users machine):

    Dim schemaUpdate As SchemaUpdate = New SchemaUpdate(NhibernateConfiguration)
    schemaUpdate.Execute(False, True)

That was the problem. The next logical question to ask is then how do you execute sql using SchemaUpdate. The answer... you can't. See this post: https://forum.hibernate.org/viewtopic.php?f=6&t=969584&view=next

Alas I am left to use raw sql. Maybe some day they will add an <update> tag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top