views:

52

answers:

2

hey

I have a collection of ReportColumns in a Report object.

The ReportColumns has a DisplayOrder field which sets where abouts in the report the column is displayed. these columns are re-orderable in the designer ui and i can write some hacky code to change the order of them - but was wondering if there's anyway in nhib to take care of re-ordering entities based on a column? or perhaps a standard pattern to follow? seems like a fairly standard thing to want to do.

w://

A: 

I don't know if I understand the question correctly but : You can ask to NH to retrieve the colums in the order specified by a column (even if it's not mapped) :

HasMany(x => x.Columns)
                .Inverse()
                .Cascade.All()
                .Not.LazyLoad()
                .OrderBy("DisplayOrder")

Here the ReportColumn objects in the Columns property will in the order specified by the OrderBy.

In HBM it will be the order-by attribute of your collection :

http://nhforge.org/doc/nh/en/index.html#collections-mapping

Matthieu
this orders the items by their displayorder column.i was talking about ordering and re-ordering - so say if i took the item at index 5 and droped it after the item at index 9 - i need the display orders to be updated.w://
cvista
A: 

ended up using a trigger:

IF EXISTS (SELECT name FROM sysobjects
      WHERE name = 'colmodels_insupd' AND type = 'TR')
   DROP TRIGGER employee_insupd
GO
CREATE TRIGGER colmodels_insupd
ON ColModels
FOR INSERT, UPDATE
AS

update cm
set displayorder = a.rownumber
from
colmodels cm
inner join
(
select colmodels.id, row_number() OVER(partition by colmodels.reportid ORDER BY colmodels.displayorder, colmodels.id desc) as rownumber
from colmodels inner join inserted 
on colmodels.reportid = inserted.reportid and  colmodels.deleted = 0 and colmodels.hidden = 0 
) a
on cm.id = a.id
cvista