Unique column combination

15 views
Skip to first unread message

nerdia

unread,
May 29, 2012, 11:35:43 AM5/29/12
to nhusers
Hi,

I was wondering if anyone could help me figure out how to setup a
constraint from the hibernate XML.

I need the combination of two columns to be unique.

So for instance, if the columns are Name and Type, Then you could have
the following combinations in the DB:

Name Type
John Leader
John Follower
Sam Leader

I can add this to the DB directly with constraints:
ALTER TABLE [Members] ADD CONSTRAINT [UQ_NameType] UNIQUE
NONCLUSTERED
(
[Name],[Type] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY
= OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [MAIN]

But am not clear how to set it up in the XML.

Any help would be appreciated.

Thanks,

Nadia

sbohlen

unread,
May 29, 2012, 12:01:13 PM5/29/12
to nhu...@googlegroups.com
You want the <database-scope> mapping element; see http://ayende.com/blog/3947/nhibernate-mapping-database-object for an example of this in action.

Cheers,
-Steve B.

sbohlen

unread,
May 29, 2012, 12:02:22 PM5/29/12
to nhu...@googlegroups.com
Sorry, that's <database-object>, of course (was working on an issue re: scoping elsewhere when I saw your post come through and so I apparently had 'scope -on-the-brain' when typing my reply :)

-Steve B.

Ricardo Peres

unread,
May 29, 2012, 4:45:24 PM5/29/12
to nhu...@googlegroups.com
natural-id is used for creating unique indexes over one or multiple columns:  http://ayende.com/blog/4061/nhibernate-natural-id.

RP

Nexus

unread,
May 30, 2012, 12:41:35 AM5/30/12
to nhusers
Although you could try to use natural-id ( caching consequence) I
think unique-key is the one you are looking for see
http://www.benday.com/2006/04/02/howto-create-a-unique-constraint-using-nhibernate/
for more info

On 29 mei, 22:45, Ricardo Peres <rjpe...@gmail.com> wrote:
> natural-id is used for creating unique indexes over one or multiple
> columns:  http://ayende.com/blog/4061/nhibernate-natural-id.
>
> RP
>
>
>
>
>
>
>
> On Tuesday, May 29, 2012 5:02:22 PM UTC+1, sbohlen wrote:
>
> > Sorry, that's <database-object>, of course (was working on an issue re:
> > scoping elsewhere when I saw your post come through and so I apparently had
> > 'scope -on-the-brain' when typing my reply :)
>
> > -Steve B.
>
> > On Tuesday, May 29, 2012 12:01:13 PM UTC-4, sbohlen wrote:
>
> >> You want the <database-scope> mapping element; see
> >>http://ayende.com/blog/3947/nhibernate-mapping-database-objectfor an
Reply all
Reply to author
Forward
0 new messages