Created
September 15, 2012 14:37
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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