Ordering a Child Table Column In NHibernate

The following post is from our development engineer Jorge Cortillo.

Recently I ran into some issues when I was trying to order a column from a child table.  The relationship between Parent table and Child table is going to be represented as a Foreign Key (also this relationship could be referenced without using a Foreign Key constraint, this scenario most of the time occurs in legacy databases) and in our mapping file we are going to have something like this:

ParentTable.hbm.xml

<bag name="ChildTable" inverse="true" lazy="false" cascade="all">
<key column="ParentID" />
<one-to-many />
</bag>

ChildTable.hbm.xml

<many-to-one name="ParentTable" column="ParentID" />

Our Properties are going to have the same reference:

ParentTable.cs

public IList<ChildTable> ChildTableList { get; set; }

ChildTable.cs

public ParentTable ParentTable { get; set; }

If you notice I’m not using “virtual” after public that’s because I declared my mapping files as default-lazy = false, so any proxy is going to be created.

Now let’s say that we have, besides this relationship, a couple of properties at each POCO:

ParentTable

Int ParentID
String Description
IList<ChildTable> ChildTableList

ChildTable

Int ChildID
String FirstName
String LastName
ParentTable ParentTable

Well, now that we have a clear scenario, let’s go with our problem and how I solved it.  Let’s say that the Business User requests that you have to order the results of ParentTable ordered by FirstName and LastName (from ChildTable).  At first, if you think about a sql statement it should be pretty simple, because you just need to add an Inner Join then add an Order By ChildTable.FirstName ASC, ChildTable.LastName ASC.  But how can we translate that into NHibernate? The answer is really simple.  First, you need to explicitly declare the join.  Then proceed to add the order statement, like this:

Session.CreateCriteria<ParentTable>("pt").CreateCriteria("ChildTable", "ct", JoinType.LeftOuterJoin);
Notice that the JoinType can be LeftOuterJoin, RightOuterJoin or InnerJoin.  And finally proceed to add the order statement like this:
criteria.AddOrder(Order.Asc("ct.FirstName"));                           
criteria.AddOrder(Order.Asc("ct.LastName"));

That’s all the code you need if you want to order a list from a child table.  If you want to return a list from a ParentTable ordered by any of its columns, you don’t have to explicitly declare the JoinType.  NHibernate is going to do that for you.  In this case, you’ll only need add AddOrder(Order.Asc(“columnName”)) and you can use Order.Desc also.

You can add more references if you want to order your result query by any child table.  It’s not limited to only ParentTable – One Child Table.

Enjoy!

Understanding The Need For Servers With Mobile Apps

I was looking into different task management solutions and recently started using iProcrastinate on my Mac.  Let me say up front that I most definitely procrastinate.  I’ve been writing the same book for several years and I rarely get my taxes done on time!

iProcrastinate looks good so far.  It has the right level of functionality and the interface is intuitive enough.  I grabbed the iPhone version and proceeded to sync.  This was not so easy.  My desktop Mac is on a LAN and I had to go wireless to sync even though my iPhone was docked.  Say that again?

I soon realized though that this made sense for an application running on different devices when there is no central server.  My systems needed a direct connection to communicate and that can only be done with devices on the same network if there is no other central means to establish the connection.  I remembered that I did not have to complete a sign up process to get started, which in truth was a nice simplification.  I would either have to connect my iPhone to my LAN (not sure that is even possible) or add my Mac to the wireless.

Let’s dive into the role of servers and the whole sync issue for a bit.  For my devices, I have 3 ways to synchronize content:

File transfer – Files are transferred between the devices directly.  Launch the  iTunes application with your device connected and click the Apps tab after selecting the device.  You see all the apps sharing files with your device at the bottom of the page.

Central server – A central server synchronizes all data and files between various systems.  The master at this is DropBox.  Install DropBox and watch how changes on one device are reflected on the others.  Evernote.com is another good example.  Record a voice memo on your iPhone and then listen to it on Evernote.com.  Both of these examples use a file as the base transfer unit, but there is a lot more information being shared including accounts, meta-data, etc.

Direct sync – This is the third case best illustrated by iProcrastinate.  You don’t need iTunes or a central server.  The two devices, in my case my iPhone and desktop, set up a direct connection or socket after which information is transferred automatically. This is slightly better than straight file transfer as more than just files can be shared.  It also has benefits over a central server including no need for an account or even an outside Internet connection to sync.

My favorite approach is the central server and not just because I am a server guy.  I like to sync from anywhere whether I am in my office or traveling in Costa Rica.  I really don’t mind creating an account although I hate it when my account information is shared with another service.  A central server provides solid functionality including:

  • Centralized backup of my content
  • Support for multiple devices
  • Anytime access to my data and information
  • Security through account management

The primary reason developers don’t use a server is cost/effort.  After all, file transfer comes for free if you use that approach.  You don’t really have to do anything.  Data sync requires development time.  Once launched, however, there are no on-going costs or servers to support.  The server represents a commitment to users to keep the application running in a secure hosted environment that must be available on a regular basis if it is to have any value.

In my opinion, the server architecture is the ideal approach for supporting solutions that require data sharing and synchronization.  Server side languages and solutions are cheap enough and easy enough where a reasonable developer can develop a solution without too much effort or time.  My favorite combination right now is Ruby on Rails hosted at Rackspace.  In the end, there are other ways to support disjoint data, but none of them are as powerful and capable as a server-based solution.

Data Modelling Tips

Here is a post from our engineer José Luis.

A few days ago, I finalized a project where I did a complete database model and design.  I would like to share with you my best tips that I think are the most relevant when you are creating a database model:

  • Databases play a main role in application development  as data repository, generally implemented as a relational database.  Considering other options  such as record sets, flat files or custom file formats, none of these are robust or safe as a database.
  • Data modeling is the process of mapping real world information to logical representations of data. Here it is important to think about data from logical standpoint without being concerned with how tables and columns will in your database.
  • Keep in mind that a bad database design will inevitably create a severe scalability problem and is often extremely difficult to modify as time goes on.
  • Describe your data model as the map containing all the pieces of information needed by you application and how data relate to one another. It also provides a non-technical users view of data and can be very useful to gain user acceptance of the overall application design. When data model is properly designed, it becomes the platform agnostic logical “blueprint” for a physical database to be developed.
  • A good data model will provide better performance to your RDBMS by following standard data modeling rules to eliminate data anomalies such duplicating data. Also data accessibility makes data easy to understand by defining entities and tables that categorize the data you are working with, then analyzing your data reporting or data warehousing will be considerably easier.
  • Understand and apply the “building blocks” in your data model by understanding entities, attributes, relationships and domains.
  • As you already know, the Normalization process is another key task in data modeling and it defines as the process of grouping data in a logical way to avoid duplication and data complexity.
  • Normal forms are named simply according to their occurrence in the sequence. Typical First, Second and Third normal forms (1NF, 2NF and 3NF) are applied as progressive forms of a data model, each one stricter than previous. Also consider the Boyce-Codd Normal Form (BCNF) and the 4NF (primary key without multiple value dependency) and 5NF (ternary relationships).
  • Evaluate if you need to apply Denormalization to your data model for punctual situations like extensive reporting and massive warehousing.
  • Always handle your data model file versioned with tools like CA Erwin, Sybase Power Designer, Embarcadero ER/Studio, MySQL Workbench, or Oracle Designer.

I hope you can consider the topics described above and get better data models that will be the root for robust and top-notch data repositories.

2010 in review

The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here’s a high level summary of its overall blog health:

Healthy blog!

The Blog-Health-o-Meter™ reads This blog is on fire!.

Crunchy numbers

Featured image

A Boeing 747-400 passenger jet can hold 416 passengers. This blog was viewed about 4,700 times in 2010. That’s about 11 full 747s.

 

In 2010, there were 25 new posts, growing the total archive of this blog to 59 posts. There were 75 pictures uploaded, taking up a total of 25mb. That’s about a picture per week.

The busiest day of the year was July 2nd with 213 views. The most popular post that day was About Avantica Technologies.

Where did they come from?

The top referring sites in 2010 were avantica.net, blog.expertceo.com, facebook.com, openmountain.com, and twitter.com.

Some visitors came searching, mostly for will apple overtake microsoft, lessons learned example, and avantica.

Attractions in 2010

These are the posts and pages that got the most views in 2010.

1

About Avantica Technologies May 2008

2

Visiting Avantica’s Northern Development Center November 2010

3

Run Mac OS X on PC Hardware July 2010

4

Design Lessons Learned Example February 2010

5

Avantica Mobile Development Leader Ronald Hernandez August 2010

Follow

Get every new post delivered to your Inbox.

Join 3,599 other followers