Results 1 to 4 of 4

Thread: nHibernate - Ensure data in different tables stored with different Id's

  1. #1

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    nHibernate - Ensure data in different tables stored with different Id's

    Hi,
    Got a problem after a file uploading function have been abused and now must figure out a solution on my side.

    In a nutshell, I have 5 different tables, not related to each other and my only solution is now to make sure no two records in those tables can have the same id.

    I’m using nHibernate (code mappings, not fluent) so guess if this is even possible I’d have to do some configuration in there).

    For those curious: I store images in a different table. One table for any images. You store an image by passing in the image, its id (in one of the above tables) and the image type.
    However the jnr. developer created a mobile app that save all images with the same type and the mobile app cannot be changed.
    Now my only work around is to make sure my api does not save objects by the same id, so I can be sure when I retrieve a certain image type by this or that id, I won’t get images for records in different tables.


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: nHibernate - Ensure data in different tables stored with different Id's

    So you're saying that two records in two different tables mustn't have the same ID, correct? If so then you probably have two options:

    1. Use a PK column with an auto-generated integer value, e.g. identity in SQL Server, and set the seed value for each table such that their ranges will not overlap, e.g. 1 for the first table, 1,000,001 for the second table, 2,000,001 for the third table and so on. Obviously the seed for each table will have to be sufficiently greater then the previous one that there won't be enough records to fill the entire range.

    2. Use a PK column that contains a GUID, e.g. uniqueidentifier for SQL Server. GUIDs are not guaranteed to be unique but the idea is that there are so many possible values that the possibility of a value being repeated is statistically insignificant.

  3. #3

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: nHibernate - Ensure data in different tables stored with different Id's

    I like the using GUID as id idea. (dang, i wanted to go with that in the beginning)
    You just educated me. I made the assumption GUIDs are unique. lol
    Thanks!


  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: nHibernate - Ensure data in different tables stored with different Id's

    Quote Originally Posted by Krokonoster View Post
    I made the assumption GUIDs are unique.
    No, they're basically just a random 128-bit number. Keep in mind as well that GUIDs as IDs in databases make for slow joins.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width