Skip to content

Instantly share code, notes, and snippets.

@davybrion
Created September 15, 2012 14:33
Show Gist options
  • Save davybrion/3728233 to your computer and use it in GitHub Desktop.
Save davybrion/3728233 to your computer and use it in GitHub Desktop.
code snippets for "Using NHibernate To Persist And Query Ruby Objects" post
<class entity-name="Artist">
<id name="id" column="ArtistId" type="int">
<generator class="identity"/>
</id>
<property name="name" length="50" type="string" />
<bag name="albums" cascade="all-delete-orphan" inverse="true" >
<key column="ArtistId"/>
<one-to-many class="Album" />
</bag>
</class>
<class entity-name="Album">
<id name="id" column="AlbumId" type="int">
<generator class="identity"/>
</id>
<property name="title" length="50" type="string" not-null="true" />
<many-to-one name="artist" column="ArtistId" not-null="true" class="Artist" />
</class>
class Artist
attr_accessor :id, :name, :albums
def initialize
self.albums = System::Collections::ArrayList.new
end
def add_album(album)
self.albums.add(album)
album.artist = self
end
def remove_album(album)
self.albums.remove(album)
album.artist = nil
end
end
class Album
attr_accessor :id, :title, :artist
end
using (var session = sessionFactory.OpenSession())
{
var artist = ruby.Artist.@new();
artist.name = "Rage Against The Machine";
var album1 = ruby.Album.@new();
album1.title = "Rage Against The Machine";
var album2 = ruby.Album.@new();
album2.title = "Evil Empire";
artist.add_album(album1);
artist.add_album(album2);
session.Save("Artist", artist);
session.Flush();
artistId = artist.id();
}
NHibernate: INSERT INTO Artist (name) VALUES (@p0); select SCOPE_IDENTITY();@p0 = 'Rage Against The Machine' [Type: String (50)]
NHibernate: INSERT INTO Album (title, ArtistId) VALUES (@p0, @p1); select SCOPE_IDENTITY();@p0 = 'Rage Against The Machine' [Type: String (50)], @p1 = 355 [Type: Int32 (0)]
NHibernate: INSERT INTO Album (title, ArtistId) VALUES (@p0, @p1); select SCOPE_IDENTITY();@p0 = 'Evil Empire' [Type: String (50)], @p1 = 355 [Type: Int32 (0)]
private static void PrintArtistData(dynamic artist)
{
Console.WriteLine("Artist: " + artist.name());
PrintAlbumData(artist.albums());
}
private static void PrintAlbumData(dynamic albums)
{
foreach (dynamic album in albums)
{
Console.WriteLine("\tAlbum: " + album.title());
}
Console.WriteLine();
}
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: 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())
{
dynamic artist = ruby.ObjectFactory.create_proxy_from_nhibernate_hash(session.Load("Artist", artistId), "Artist", artistId);
Console.WriteLine("display output from session.Load");
PrintArtistData(artist);
}
display output from session.Load
NHibernate: SELECT artist0_.ArtistId as ArtistId0_0_, artist0_.name as name0_0_ FROM Artist artist0_ WHERE artist0_.ArtistId=@p0;@p0 = 355 [Type: Int32 (0)]
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment