X

Using NHibernate order-by attribute to sort names history

I am writing application that besides other cool things lets users insert information about business contacts. My system is able to keep names history of contacts. In this posting I will show you how to use NHibernate mappings to keep names ordered logically.

Here is fragment of my class diagram. It is working draft, so don’t take it as an example of perfect solution. (To find out more about class structure represented here read my blog posting Modeling people and organizations: Class Party).

Here is what I want to achieve. I took this screenshot from my working draft.

The problem here is sort order of valid-to dates. If I just sort names by date to descending order then current name (we don’t know how long it is valid) is the last one. I needed order shown on the image above.

Here is how I achieved this order using NHibernate mapping file and MSSQL COALESCEfunction. The trick is simple – I used COALESCE to replace null with current date.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <joined-subclass
    name="OM.Core.Contacts.Company, OM.Core"
    extends="OM.Core.Contacts.Party, OM.Core"
    table="company"
  >
    <key column="party_id" />
    <property name="RegistryCode" column ="registry_code"/>
    <!-- more properties here -->
 
    <bag name="Names" table="company_names" cascade="all" 
      order-by="COALESCE(valid_to, GETDATE()) desc">
      <key column="company_id"/>
      <one-to-many class="OM.Core.Contacts.CompanyName, OM.Core"/>
    </bag>
  </joined-subclass>
</hibernate-mapping>

I think it is good solution for sorting because sorting is done in database and we don’t have to write additional code to DAL to get collection sorted. Of course, it is not recommended to change (reassign, reorder) collections monitored by NHibernate. What I did here was simple – I just added order-by attribute to Names bag definition and let NHibernate do the rest.

Liked this post? Empower your friends by sharing it!
Categories: Data platform

View Comments (7)

  • Thanks for the tip. I have actually been in the same situation, ending up doing much more dirty approach.

  • Neat solution but I'm not crazy about it for two reasons: 1) It's database specific and 2) Display order is the UI's concern, not the model's. Point 2 is the bigger worry because the sort is only in place when loading, it may be lost if the collection changes in memory.

  • Thanks for feedback, Jamie! :)

    1) there are always database specific things while databases have differences
    2) nothing sais you cannot use different sort order or sorting in UI - it is up to you how you like to solve it.

    In my case one sort order is enough and as it is web application new data is requested after each modification. In my case it is not problem because names table is not very large (when compared to some other tables).

  • “It’s a easy element that can save time in the long term.” - Moe. These ideas actually are really simple to get to homeowners with. If you're dishonest from the start, then chances are high you is not going to meet the proper accomplice for you. There are various assets on the net that can assist you with the varied dating advice to assist you discover the proper partner for you. For such individuals such dating tips will serve to be of nice assist in building the connection and likewise in finding the best form of companion for you. In different phrases, you could also be an awesome man with lots to supply, however in case you can’t crack the code and know what goes into a terrific first date, will probably be onerous to attract superb ladies. And when he falls for that truly fantastic girl, and she falls for him, he can settle down and create an ideal relationship. However, each first date could possibly be the start of an extended and glad relationship. However, ladies who are wanting forward up to now partners who are older than them need to study some basic suggestions. Do some background research on your match - primarily based on the information you have got - to see if they are who they are saying they are.

    Plan forward: if you and your match want to meet up in individual, arrange a date in a public place so there are people around in case you need assistance. Listed here are 10 first date ideas my shoppers use to have wonderful first dates with amazing ladies. Some people have issues lined up right after their dates. Be sure to explore if the person is best for you.” - Moe. “The finest first dates are about opening up, connecting, and sharing what’s actually going on with you.” - Logan. Study spam, viruses and bots: keep away from opening emails or attachments from senders you don’t acknowledge. Don’t settle for follower requests on your personal social media accounts from folks you don’t know. This fashion you’ll appeal to individuals looking for a similar thing. They offer the possibility to get to know like-minded folks in search of love and friendship latamdate.com in the native area and beyond. And then he went into films, and he directed the Muppets film, and LSU the wanting glass with Johnny Depp. Then he began to share his story of a earlier relationship and when he received in he used to turn toward Cooper for solutions.

    I do know that it hurts suppose about the relationship as well as your ex & perhaps it is tough to go on each day actions. I hope you found worth in this publish, searching for knowledge in relationships is a each day observe. A professional Online Dating Web site bears genuine info of the users and moreover, they value the privateness of the users. And get your profile Photo and ID verified as a result of Spring is for matching with different verified users who also have their shit collectively. Create a person identify that reflects who you're. Also, concentrate on the dialog you’re having, not what those subsequent to you are speaking about. Also, it gives you an opportunity to talk and socialize with the bartender and with individuals round you. Dating agency Dublin provides you precious dating advice that may help you impress and win you the love you might be pondering of. You get the important level: you’re solely justified in ignoring the deluge of romantic recommendation chances are you'll encounter in the next few days and for the rest of the year. And I'm in no doubt that the intentions behind imparting this nugget of advice are honourable.

    Men and women identical to you might be studying to make use of technology to assist them find that particular someone later in life. We've all had moments the place we would have liked a cold arduous dose of reality to help us move on from somebody who wasn't matching our power, and wasn't treating us the way in which we'd wish to be treated. If somebody you met on-line is blackmailing you, sending you sexually express materials or doing something else you’re not Ok with, it’s vital to tell a protected adult. Be certain to tell a friend or secure adult the place you’re going, who you’re assembly and what time you’ll be performed. Tell stories that reveal issues about yourself, and actively hear when your date does the identical. You must search for articles and ideas that will enable you to date in the residing section of the newspaper in the morning, and in various magazines, equivalent to Glamour. It will assist her chill out, too, as a result of you’ll really feel extra assured and comfy. They’re the particular person you always really feel snug round, it doesn't matter what, who's at all times in your side, it doesn't matter what. A sentimentally generous person is any person who is definitely thoughtful, versatile, sensitive and sort to ad to others.

Related Post