These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: unique-key, but not primary key
PostPosted: Thu Feb 12, 2009 3:34 pm 
Newbie

Joined: Thu Feb 05, 2009 7:31 pm
Posts: 9
I've got an easy question, i hope i will be luckier than latest time i asked a generical quesiton in this forum (although i know that my problem was not easy to be understood)

In order to avoid boring you with my particular classes, i'm exposing my current situation with a similar example

Let's assume the following class:
Code:
Class Student
{
         public int StudentId;
         public string FirstName;
         public string MiddleName;
         public string SecondName;
}


I'm assuming that in case StudentId were primary key, mapping should seem like this:

Code:
<class name="Student" table="STUDENTS">
      <id name="StudentId">
          <column name="StudentID"/>
          <generator class="native"/>
      </id>
      <property name="FirstName"/>
      <property name="MiddleName"/>
      <property name="SecondName"/>
</class>


But, what if i wanted to make FirstName/MiddleName/SecondName an unique index?

I've tried with following:

Code:
<class name="Student" table="STUDENTS">
      <id name="StudentId">
          <column name="StudentID"/>
          <generator class="native"/>
      </id>
      <property name="FirstName" unique-key="NAME_MUST_BE_UNIQUE"/>
      <property name="MiddleName" unique-key="NAME_MUST_BE_UNIQUE"/>
      <property name="SecondName" unique-key="NAME_MUST_BE_UNIQUE"/>
</class>

I'm using _session.SaveOrUpdate, but i still seeing unique constraints violated. After digging into forum, i've started to think that unique-key it's just intende to crate tables, but it's unuseful for already created tables processing

Does NHibernate provide any way to (let say) check FirstName MiddleName and SecondName and insert in case there were not violating unique constraing and update when they were?

I'm working on SQLExpress, but it's intende to move to Oracle in near future

Thanks for any helpful answer you can provide me.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2009 1:27 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Quote:
i've started to think that unique-key it's just intende to crate tables, but it's unuseful for already created tables processing


You're right. It's only used during schema export.

Quote:
Does NHibernate provide any way to (let say) check FirstName MiddleName and SecondName and insert in case there were not violating unique constraing and update when they were?


Not really. You either have to go and use try/catch or do the validation yourself before you insert the object. YOu can have a look at NHibernate.Validator project if they provide a more general approach.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.