Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 869273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:16:53+00:00 2026-05-15T10:16:53+00:00

Object A has a one-to-many association: many object B . When I look in

  • 0

Object A has a one-to-many association: many object B.

When I look in the database — TableB — I’d like to see the unique, readable string A.Name instead of having always to join or subselect on a surrogate integer identifier to see the name.

I can map Name as the identifier for A, but this causes lots of extra SELECT queries because NHibernate can’t identify whether an instance of A is transient or persistent.

I imagine that I could use a composite key, combining a native-assigned surrogate key with the natural key. This seems sub-optimal, but I’d love to hear some opinions.

What I’m really looking for is a strategy for using a single-column natural key while allowing NHibernate to identify transient instances.

  • Is it possible?
  • What is the mapping — fluent or hbm?

On the other hand, if this is all a terrible idea and I should just rely on database views with subselects, please explain.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:16:54+00:00Added an answer on May 15, 2026 at 10:16 am

    It’s a terrible idea and you should create a view that does the join for those occasions when you need to “look in the database”.

    It’s a terrible idea because Name isn’t the primary key of table A.

    But I think you can do it if you put a unique constraint on A.Name and map it as the identifier. I’m not sure if NHibernate defaults to null or empty string for the unsaved value but you can specify it using

    Id(x => x.Name).GeneratedBy.Assigned().UnsavedValue(string.Empty); // or null
    

    If you use string.Empty than A’s constructor must initialize Name to string.Empty. I would also remove the surrogate identifier from A and maybe the table itself since it serves no purpose.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent object which has a one to many relationship with an
Hibernate has a handful of methods that, one way or another, takes your object
How do I see if a certain object has been loaded, and if not,
We need to see what methods/fields an object has in Javascript.
I have a database populated with 1 million objects. Each object has a 'tags'
When an object has various formats (XML,CSV) it can be represented in, where should
If an object has a property that is a collection, should the object create
I regularly want to check if an object has a member or not. An
Do you generally assume that toString() on any given object has a low cost
I have a tree that consists of several objects, where each object has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.