How to create overly complex NHibernate queries?

by Mikael Henriksson 15. December 2009 16:06

If I had it my way we would probably have the whole darn thing in one and the same table but that would not work out long term. The problem I was facing was to fetch one entity based on values in the parent and 2 other (to the parent) related entities. Then I want to eagerly fetch information from the parent.

// Create criteria and add sorting based on version
var criteria = work.Session.CreateCriteria<ContactVersion>("v")
    .AddOrder(Order.Desc("v.Version"));

// Create restriction based on that the contact should:
// 1. Not be deleted and
// 2. Match the accountId
criteria.CreateCriteria("v.Contact", "c")
    .Add(Restrictions.IsNull("c.DeletedAt"))
    .Add(Restrictions.Eq("c.Id", accountId));

// Also add a restriction based on uid
criteria.CreateCriteria("c.Devices", "dc")
    .Add(Restrictions.Eq("dc.Id", uid));

// Lastly add a restriction for the current device id and
// eagerly fetch the corresponding contact.
criteria.CreateCriteria("dc.Device", "d")
    .Add(Restrictions.Eq("d.Id", deviceId))
    .SetFetchMode("c.Contact", FetchMode.Join)
    .SetFetchSize(1)
    .SetMaxResults(1);

var c = criteria.UniqueResult<ContactVersion>();

This gives me the desired result and the query looks like follows:

SELECT top 1 * /* removed all the columns */
FROM   contact_version this_
       inner join contact_def c1_
         on this_.contact_id = c1_.contact_id
       inner join contact_device_uid dc2_
         on c1_.contact_id = dc2_.contact_id
       inner join device_def d3_
         on dc2_.device_id = d3_.device_id
WHERE  this_.contact_version_ordinal = 3 /* @p0 */
       and c1_.dte_deleted is null
       and c1_.subscription_id = 4 /* @p1 */
       and dc2_.contact_uid = 1 /* @p2 */
       and d3_.device_id = 1 /* @p3 */

A quick look at the query execution plan shows nothing really strange so I suppose I sort of got it right. At least I got the data that I asked for..

Tags:

NHibernate

How to waste half a day getting R# to run unit tests…

by Mikael Henriksson 10. December 2009 14:11

Today I feel like the most retarded guy on planet earth. I have just wasted half a day trying to get R# to run my unit tests. The tests simply does not even start so they don’t fail either. 
A check of the output window shows that there is a possible chance of a FileNotFoundException which I thought was bullshit. I had a look at the settings for R# Unit Test and no there was nothing that I could do to make it work. Since I AM a smart guy usually (I am just feeling a bit stupid today) I decided to make all exceptions throw so that I could at least find some sort of trace or hint of where the problem actually occurs.

Ctrl+Alt+E takes you to the Visual Studio exceptions settings where I turn everything to throw and then I start debugging the unit tests. After a good few clicks some interesting exceptions shows up. It’s a FileNotFoundException and it looks like this:

System.IO.FileNotFoundException occurred
  Message="Could not load file or assembly 'file:///D:\\Apps\\NUnit 2.5.2\\bin\\net-2.0\\lib\\nunit.core.dll' or one of its dependencies. The system cannot find the file specified."
  Source="mscorlib"
  FileName="file:///D:\\Apps\\NUnit 2.5.2\\bin\\net-2.0\\lib\\nunit.core.dll"
  FusionLog="=== Pre-bind state information ===
  LOG: User = MOBILENORDIC\\mikael
  LOG: Where-ref bind. Location = D:\\Apps\\NUnit 2.5.2\\bin\\net-2.0\\lib\\nunit.core.dll
  LOG: Appbase = file:///D:/Projects/****.IntegrationTests/bin/Debug
  LOG: Initial PrivatePath = NULL
  Calling assembly : (Unknown).===
  LOG: This bind starts in LoadFrom load context.
  WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
  LOG: Using application configuration file: D:/Projects/****.IntegrationTests\\bin\\Debug\\*.IntegrationTests.dll.config
  LOG: Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\config\\machine.config.
  LOG: Attempting download of new URL file:///D:/Apps/NUnit 2.5.2/bin/net-2.0/lib/nunit.core.dll."
  StackTrace:
       at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
       at System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
       at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
       at System.Reflection.Assembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, StackCrawlMark& stackMark)
       at System.Reflection.Assembly.LoadFrom(String assemblyFile)
       at JetBrains.ReSharper.UnitTestProvider.nUnit.NUnitTaskRunner.ExecuteRecursive(TaskExecutionNode node) in c:\Agent\work\cc29ccea5bf569df\src\UnitTestProvider.nUnit\src\NUnitTaskRunner.cs:line 103
  InnerException: 

Well interesting, it turned out that it was actually me being stupid. I was positive this had nothing to do with R# but the level of retardness does not stop here! I go back to the R# settings and look for somewhere to change the path to nunit but can’t find anything so I rename my d:\apps\nunit folder to match the one I couldn’t find and try again and now my unit tests run perfectly. I decided the bloody thing had messed with me for the last time! Going back to the settings for Unit Tests in R# I am faced with the following picture:

resharper_unit_test_settings

Now where the heck is that darn setting??? I open up the resharper.user file in notepad2 and search for nunit and at the bottom I found this:

  <UnitTestRunnerNUnit>
    <NUnitInstallDir>D:\Apps\NUnit 2.5.2\bin\net-2.0</NUnitInstallDir>
    <UseAddins>Always</UseAddins>
    <UseSpecifiedNUnit>True</UseSpecifiedNUnit>
  </UnitTestRunnerNUnit>

Now I can change this to the folder where it is REALLY located and that is great when it hits me. I remember changing this in resharper for some stupid reason and I also remember that I did so within the Resharper Unit Test section of the options. While navigating back and clicking on nunit I am looking at the following picture:

resharper_nunit_settings

 

Either I am mentally retarded or that part of the ReSharper options is so pick your choice.

Tags: ,

FAIL | R# | Testing