Have you considered the following approach:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate;
using NHibernate.Linq;
using AutoMapper;
using FluentNHibernate.Mapping;
namespace NHTests
{
class Program
{
private static bool exportSchema = true;
static void Main(string[] args)
{
exportSchema = true;
Create3Cars();
using (var session = GetSession())
using (var transaction = session.BeginTransaction())
{
// Get cars with id in (2,3)
var cars = from car in session.Linq<Car>()
where (new int[] { 2, 3 }).Contains(car.Id)
select car;
// Blow up the front wheels :)
foreach (var car in cars)
{
car.FrontLeftWheel.Psi++;
car.FrontRightWheel.Psi++;
session.SaveOrUpdate(car);
}
transaction.Commit();
}
}
private static void Create3Cars()
{
using (var session = GetSession())
using (var transaction = session.BeginTransaction())
{
session.Save(new Car() { Model = "Ferrary" });
session.Save(new Car() { Model = "Porche" });
session.Save(new Car() { Model = "Mercedes" });
transaction.Commit();
}
}
#region Car
public class Car
{
public static readonly float DefaultPsi = 30;
public virtual int Id { get; set; }
public virtual string Model { get; set; }
public virtual ICollection<Wheel> Wheels { get; protected
set; }
public Car()
{
Wheels = new List<Wheel>
{
new Wheel { Position = WheelPosition.FrontLeft, Owner
= this, Psi = DefaultPsi},
new Wheel { Position = WheelPosition.FrontRight, Owner
= this, Psi = DefaultPsi},
new Wheel { Position = WheelPosition.RearLeft, Owner =
this, Psi = DefaultPsi},
new Wheel { Position = WheelPosition.RearRight, Owner
= this, Psi = DefaultPsi},
};
Mapper.CreateMap<Wheel,
Wheel>().ConvertUsing<WheelConverter>();
}
public virtual Wheel FrontLeftWheel
{
get { return GetWheel(WheelPosition.FrontLeft); }
set { SetWheel(value, WheelPosition.FrontLeft); }
}
public virtual Wheel FrontRightWheel
{
get { return GetWheel(WheelPosition.FrontRight); }
set { SetWheel(value, WheelPosition.FrontRight); }
}
public virtual Wheel RearLeftWheel
{
get { return GetWheel(WheelPosition.RearLeft); }
set { SetWheel(value, WheelPosition.RearLeft); }
}
public virtual Wheel RearRightWheel
{
get { return GetWheel(WheelPosition.RearRight); }
set { SetWheel(value, WheelPosition.RearRight); }
}
public override string ToString()
{
return string.Format("The car's id is {0}, it's wheels
are: \n{1}\n{2}\n{3}\n{4}\n\n",
Id, FrontLeftWheel, FrontRightWheel,
RearLeftWheel, RearRightWheel);
}
private void SetWheel(Wheel value, WheelPosition
wheelPosition)
{
Mapper.Map<Wheel, Wheel>(value,
GetWheel(wheelPosition));
}
private Wheel GetWheel(WheelPosition wheelPosition)
{
return Wheels.Where(w => w.Position ==
wheelPosition).First();
}
}
#endregion
#region CarMap
public class CarMap : ClassMap<Car>
{
public CarMap()
{
Id(c => c.Id);
Map(c => c.Model);
HasMany<Wheel>(c => c.Wheels)
.AsSet()
.Inverse()
.Cascade.All()
.KeyColumn("Owner_Id");
}
}
#endregion
#region Wheel
public enum WheelPosition
{
FrontLeft,
FrontRight,
RearLeft,
RearRight
}
public class Wheel
{
public virtual int Id { get; set; }
public virtual float Psi { get; set; }
public virtual WheelPosition Position { get; set; }
public virtual Car Owner { get; set; }
public override string ToString()
{
return string.Format("Id = {0}, Position = {1}, Psi =
{2}, OwnerId = {3}",
Id, Position, Psi, Owner.Id);
}
}
#endregion
#region WheelMap
public class WheelMap : ClassMap<Wheel>
{
public WheelMap()
{
Id(w => w.Id);
Map(w => w.Psi);
Map(w => w.Position).CustomType<WheelPosition>();
References(w => w.Owner).ForeignKey("Owner_Id");
}
}
#endregion
#region WheelConverter
public class WheelConverter : TypeConverter<Wheel, Wheel>
{
protected override Wheel ConvertCore(Wheel source)
{
return new Wheel
{
Psi = source.Psi,
};
}
}
#endregion
#region NHConfigurations
private static ISessionFactory _factory;
public static ISession GetSession()
{
if (_factory == null)
_factory = CreateSessionFactory();
return _factory.OpenSession();
}
public static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ShowSql()
.ConnectionString(cs =>
cs.FromConnectionStringWithKey("DbConnection")))
.ExposeConfiguration(AddConfigurations)
.Mappings(m => m.FluentMappings
.Add<WheelMap>()
.Add<CarMap>())
.BuildSessionFactory();
}
public static void
AddConfigurations(NHibernate.Cfg.Configuration cfg)
{
if (exportSchema)
new SchemaExport(cfg).Create(true, true);
}
#endregion
}
}
On Apr 23, 1:28 am, Datvi <[email protected]> wrote:
> That's what i am planning to do if i can't find any simple solution.
> It doesn't look like an extraordinary situation. I bleive nhibernate
> can handle this.
>
> On 22 Nisan, 20:34, nadav s <[email protected]> wrote:
>
>
>
>
>
> > not puting them into a collection will be un-optimized as it'll cause 4
> > selects instead of one returning all the wheels. You can put it in a
> > protected collection property and expose the 4 public properties
>
> > On Thu, Apr 22, 2010 at 7:21 PM, Datvi <[email protected]> wrote:
> > > Hello,
>
> > > How can i map kind of relation?
>
> > > Class Wheel
> > > {
> > > int id;
> > > Car Owner;
> > > }
>
> > > Class Car
> > > {
> > > int id;
> > > Wheel Wheel1;
> > > Wheel Wheel2;
> > > Wheel Wheel3;
> > > Wheel Wheel4;
> > > }
>
> > > I don't want to put wheels into a collection.
>
> > > I tried this fluentmapping but Wheelmap.Owner comes always null :
>
> > > Class WheelMap : ClassMap<Wheel>
> > > {
> > > Id(x=>x.Id);
> > > References(x=>x.Owner);
> > > }
>
> > > Class CarMap : ClassMap<Car>
> > > {
> > > Id(x=>x.Id);
> > > References(x=>x.Wheel1).Cascade.All();
> > > References(x=>x.Wheel2).Cascade.All();
> > > References(x=>x.Wheel3).Cascade.All();
> > > References(x=>x.Wheel4).Cascade.All();
> > > }
>
> > > Answers with xml mapping welcome aswell
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "nhusers" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<nhusers%[email protected]
> > > >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/nhusers?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group
> > athttp://groups.google.com/group/nhusers?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group
> athttp://groups.google.com/group/nhusers?hl=en.
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.