Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 15, 2012 14:37
Show Gist options
  • Save davybrion/3728258 to your computer and use it in GitHub Desktop.
Save davybrion/3728258 to your computer and use it in GitHub Desktop.
code snippets for "Using NHibernate To Persist And Query Ruby Objects" post, part II
using (var session = sessionFactory.OpenSession())
{
var artistHash = session.CreateCriteria("Artist")
.Add(Restrictions.IdEq(artistId))
.SetFetchMode("albums", FetchMode.Join)
.List()[0];
dynamic artist = ruby.ObjectFactory.create_from_nhibernate_hash(artistHash);
Console.WriteLine("display output from session.CreateCriteria without any lazy loading");
PrintArtistData(artist);
}
NHibernate: SELECT this_.ArtistId as ArtistId0_1_, this_.name as name0_1_, albums2_.ArtistId as ArtistId3_, albums2_.AlbumId as AlbumId3_, albums2_.AlbumId as AlbumId1_0_, albums2_.title as title1_0_, albums2_.ArtistId as ArtistId1_0_ FROM Artist this_ left outer join Album albums2_ on this_.ArtistId=albums2_.ArtistId WHERE this_.ArtistId = @p0;@p0 = 355 [Type: Int32 (0)]
display output from session.CreateCriteria without any lazy loading
Artist: Rage Against The Machine
Album: Rage Against The Machine
Album: Evil Empire
using (var session = sessionFactory.OpenSession())
{
var artistHash = session.CreateCriteria("Artist")
.Add(Restrictions.IdEq(artistId))
.List()[0];
dynamic artist = ruby.ObjectFactory.create_from_nhibernate_hash(artistHash);
Console.WriteLine("display output from session.CreateCriteria with lazy loading of albums");
PrintArtistData(artist);
}
NHibernate: SELECT this_.ArtistId as ArtistId0_0_, this_.name as name0_0_ FROM Artist this_ WHERE this_.ArtistId = @p0;@p0 = 355 [Type: Int32 (0)]
display output from session.CreateCriteria with lazy loading of albums
Artist: Rage Against The Machine
NHibernate: SELECT albums0_.ArtistId as ArtistId1_, albums0_.AlbumId as AlbumId1_, albums0_.AlbumId as AlbumId1_0_, albums0_.title as title1_0_, albums0_.ArtistId as ArtistId1_0_ FROM Album albums0_ WHERE albums0_.ArtistId=@p0;@p0 = 355 [Type: Int32 (0)]
Album: Rage Against The Machine
Album: Evil Empire
using (var session = sessionFactory.OpenSession())
{
var albumsList = session.CreateCriteria("Album")
.CreateAlias("artist", "a", JoinType.InnerJoin)
.SetMaxResults(5)
.List();
dynamic albums = ruby.ObjectFactory.create_multiple_from_nhibernate_list(albumsList);
foreach (dynamic album in albums)
{
Console.WriteLine(string.Format("'{0}' by '{1}'", album.title(), album.artist().name()));
}
}
NHibernate: SELECT TOP (@p0) this_.AlbumId as AlbumId1_1_, this_.title as title1_1_, this_.ArtistId as ArtistId1_1_, a1_.ArtistId as ArtistId0_0_, a1_.name as name0_0_ FROM Album this_ inner join Artist a1_ on this_.ArtistId=a1_.ArtistId;@p0 = 5 [Type: Int32 (0)]
'For Those About To Rock We Salute You 2' by 'Accept'
'Balls to the Wall' by 'Accept'
'Restless and Wild' by 'Accept'
'Let There Be Rock' by 'AC/DC'
'Big Ones' by 'Aerosmith'
using (var session = sessionFactory.OpenSession())
{
dynamic artist = ruby.ObjectFactory.create_from_nhibernate_hash(session.Get("Artist", artistId));
artist.name = "RATM";
artist.albums()[1].title = "The Battle Of Los Angeles";
artist.remove_album(artist.albums()[0]);
dynamic newAlbum = ruby.Album.@new();
newAlbum.title = "Renegades";
artist.add_album(newAlbum);
session.Flush();
}
using (var session = sessionFactory.OpenSession())
{
dynamic artist = ruby.ObjectFactory.create_from_nhibernate_hash(session.Get("Artist", artistId));
Console.WriteLine("display output from session.Get");
PrintArtistData(artist);
}
NHibernate: SELECT artist0_.ArtistId as ArtistId0_0_, artist0_.name as name0_0_ FROM Artist artist0_ WHERE artist0_.ArtistId=@p0;@p0 = 355 [Type: Int32 (0)]
display output from session.Get
Artist: RATM
NHibernate: SELECT albums0_.ArtistId as ArtistId1_, albums0_.AlbumId as AlbumId1_, albums0_.AlbumId as AlbumId1_0_, albums0_.title as title1_0_, albums0_.ArtistId as ArtistId1_0_ FROM Album albums0_ WHERE albums0_.ArtistId=@p0;@p0 = 355 [Type: Int32 (0)]
Album: The Battle Of Los Angeles
Album: Renegades
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment