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.  [ 3 posts ] 
Author Message
 Post subject: Problem with unique key and insert
PostPosted: Sun Mar 08, 2009 11:51 pm 
Newbie

Joined: Sun Mar 08, 2009 11:48 pm
Posts: 3
Location: Sweden
I've quite recently discovered NHibernate, so forgive me if this is a beginners question, but I can't find the answer anywhere on Google.

I have three tables, two entity tables and one relation table.
Code:
BlogPosts:
   Id Identity (int)
   Title (nvarchar)
   Content (nvarchar)

Categories
   Id Identity (int)
   Name (nvarchar)

BlogPostInCategory
   Category (int) Foreign key to Categories
   BlogPost (int) Foreign key to BlogPosts


These are modeled in C# like this.
Code:
class BlogPost {
   public virtual int Id { get; set; }
   public virtual string Title { get; set; }
   public virtual string Content { get; set; }
   public virtual ISet<Category> Categories { get; set; }
}

class Category {
   public virtual int Id { get; set; }
   public virtual string Name { get; set; }
}


That displays the relation in code that I want.
I want to be able to select BlogPosts in a certain Category or get all Categories that a certain BlogPost is in.

Category is mapped as an usual entity with Unique key on the Name property.
BlogPost is mapped as usual, but the Categories property is a <set> with <many-to-many> like this.
Code:
<set name="Categories" table="`BlogPostInCategory`" cascade="save-update">
   <key column="BlogPost"/>
   <many-to-many column="Category"
       class="Category, Assembly"/>
</set>


The problem is the following, when doing inserts (I add new Category to the Categories collection) I get exceptions that there already is a Category with that name (Duplicate unique key). How can I tell NHibernate to check before doing insert and if there's a value, it should use that Id instead of trying to insert? This is really bothering me since this isn't a very complex technique. It's just normal normalization and common sense.

I appreciate the help.

Kind regards, Kim Johansson


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 11:54 am 
Newbie

Joined: Sun Mar 08, 2009 11:48 pm
Posts: 3
Location: Sweden
I think I've solved it. I use the same category object for each BlogPost I insert in the test, and then later on I just go through the Categories and check which is already in the database manually.

It may not be nice, but it works.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 12:07 pm 
Regular
Regular

Joined: Wed Feb 11, 2009 10:58 am
Posts: 55
You could get yourself a map/dictionary of all categories.

Something like:

Code:
public virtual Dictionary<String,Category> { get; set; }


where the String Parameter is the Category Name.

Mapping file would look something like the following:

Code:
<map name="Categories" table="CATEGORY_TABLE" lazy="true" >
      <key column="CATEGORY_ID" />
      <index-many-to-many class="String" column="CATEGORY_NAME"/>
      <one-to-many class="Category"/>
</map>


When you're inserting a new Blogpost you can use this Dictionary to look up if there's already an entry with your unique Category Name.

If that doesn't fit your needs maybe you can have a look at Lifecycle Callbacks (chapter 4.4 in the nhibernate manual).

Another idea might be to make the Category Name the primary key (though this is not very nice).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.