I've been able to manually "marry up" Fluent NHibernate with
NHibernate 2.1 (a long involved process that already exceeds 5 pages
of steps). Even after doing so this issue remained until I changed
storemap.cs to this:
namespace Examples.FirstProject.Mappings
{
public class StoreMap : ClassMap<Store>
{
public StoreMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasManyToMany(x => x.Products)
.Cascade.All()
.WithTableName("StoreProduct");
HasMany(x => x.Staff)
.Inverse()
.Cascade.All()
;
}
}
}
To prove to myself that it was working with the above change (and also
to prove that it fails without that change), I changed method main to
separate out the session that writes the objects from the one that
retrieves them. Some partial code snippets follow:
using (var session = sessionFactory.OpenSession())
{
// populate the database
using (var transaction = session.BeginTransaction())
{
// create a couple of Stores each with some
Products and Employees
var barginBasin = new Store { Name = "Bargin
Basin" };
var superMart = new Store { Name = "SuperMart" };
<snip>
// save both stores, this saves everything else
via cascading
session.SaveOrUpdate(barginBasin);
session.SaveOrUpdate(superMart);
transaction.Commit();
}
session.Close();
}
// Do the Employees get saved when we save the Store
instance that they work in ?
using (var session = sessionFactory.OpenSession())
{
// retreive all stores and display them
using (session.BeginTransaction())
{
var stores = session.CreateCriteria(typeof(Store))
.List<Store>();
foreach (var store in stores)
{
WriteStorePretty(store);
}
}
Console.ReadKey();
}
}
My version of CreateSessionFactory looks like this (note the use of
ProxyFactoryFactory) and required adding an explicit Reference to the
NHibernate.ByteCode.LinFu assembly in order to make it work correctly:
/// <summary>
/// Sets up the required NHibernate session factory
/// </summary>
/// <returns>ISessionFactory instance</returns>
private static ISessionFactory CreateSessionFactory()
{
string connString = GetDatabaseConnectionString();
Console.WriteLine(connString);
ISessionFactory returnValue = null;
// Use Fluent NHibernate instead of an NHibernate XML
config file
returnValue = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.Is(connString))
.ShowSql()
.DefaultSchema("dbo")
.ProxyFactoryFactory
("NHibernate.ByteCode.LinFu.ProxyFactoryFactory,
NHibernate.ByteCode.LinFu")
)
.Mappings(m => m
.FluentMappings.AddFromAssemblyOf<Program>()
)
//WARNING: will DROP/CREATE
tables .ExposeConfiguration (BuildSchema)
.BuildSessionFactory()
;
return returnValue;
}
The above also required my adding the following lines to
MsSqlConfiguration.cs to take advantage of NHibernate 2.1 support of
SQL Server 2008:
public static MsSqlConfiguration MsSql2008
{
get { return new MsSqlConfiguration
().Dialect<MsSql2008Dialect>(); }
}
On Mar 14, 9:48 pm, kellyb <[email protected]> wrote:
> Bless you all for making this library - I love it.
>
> I'm experiencing this same problem. The many-to-many seems to be
> working fine, but not the one-to-many
>
> On Mar 4, 7:57 am, x97mdr <[email protected]> wrote:
>
>
>
> > Ha! If it can be fixed that would be great, any help is much
> > appreciated.
>
> > ... and thanks for taking the time to do this project in the first
> > place. Making open source software is a labor of love! It needs some
> > recognition once in a while so here's the recognition :)
>
> > On Mar 3, 5:54 pm, James Gregory <[email protected]> wrote:
>
> > > Take the lack of a response as an acknowledgement of the bug. I need not
> > > reminding about it's existence, just the time to fix it.
>
> > > On Tue, Mar 3, 2009 at 9:24 PM, fmorriso <[email protected]> wrote:
>
> > > > FYI: still not fixed using the latest Fluent NHibernate code (last
> > > > modified Feb 26, 2009).
>
> > > > On Feb 18, 6:48 pm, x97mdr <[email protected]> wrote:
> > > > > *bump*
>
> > > > > Just wondering if this issue was ever resolved?
>
> > > > > On Feb 15, 3:30 pm, fmorriso <[email protected]> wrote:
>
> > > > > > For what it's worth, here's the code for my temporary work-around.
> > > > > > Put it after this line in Program.cs:
>
> > > > > > var joan = new Employee { FirstName = "Joan", LastName = "Pope" };
>
> > > > > > // added by Fred Morrison because Store objects
> > > > > > don't seem to save the Employee's
> > > > > > // that work in the store.
> > > > > > var employees = new List<Employee>() { daisy,
> > > > > > jack, sue, bill, joan };
> > > > > > foreach (var employee in employees)
> > > > > > {
> > > > > > session.SaveOrUpdate(employee);
> > > > > > Console.WriteLine(employee.Id);
> > > > > > }
>
> > > > > > On Feb 12, 5:56 pm, James Gregory <[email protected]> wrote:
>
> > > > > > > Somebody else just raised this exact issue today, I'll investigate
> > > > asap.
> > > > > > > Thanks.
>
> > > > > > > On Thu, Feb 12, 2009 at 9:20 PM, fmorriso <[email protected]>
> > > > wrote:
>
> > > > > > > > I'm running the Examples.FirstProject using Microsoft SQL Server
> > > > 2008
> > > > > > > > Developer Edition and Visual Studio 2008 SP1, .Net Framework 3.5
> > > > SP1
> > > > > > > > and it does not generate any INSERT's for the Employee table.
>
> > > > > > > > The narrative for the example indicates that saving a Store
> > > > > > > > object
> > > > > > > > should auto-generate any necessary Employee INSERT's, but the
> > > > output
> > > > > > > > on the console shows only INSERT's for Store, Product and
> > > > > > > > StoreProduct. A SELECT * FROM dbo.[Employee] returns zero rows.
>
> > > > > > > > There aren't very many changes needed to switch the example to
> > > > > > > > use
> > > > SQL
> > > > > > > > Server 2008, but perhaps one or more of those changes I made
> > > > > > > > could
> > > > > > > > have disabled the automatic generation of Employee INSERT
> > > > > > > > statements? I hate to post tons of code, so if somebody could
> > > > email
> > > > > > > > me to show me how to upload the code, I'd be glad to share it -
> > > > > > > > and
> > > > > > > > maybe learn something in the process.
>
> > > > > > > > Here's just a small example of a change I made to allow the
> > > > > > > > example
> > > > to
> > > > > > > > use SQL Server 2008. Hopefully, I didn't mess it up too bad:
>
> > > > > > > > /// <summary>
> > > > > > > > /// Sets up the required NHibernate session factory
> > > > > > > > /// </summary>
> > > > > > > > /// <returns>ISessionFactory instance</returns>
> > > > > > > > private static ISessionFactory CreateSessionFactory()
> > > > > > > > {
> > > > > > > > string connString = GetDatabaseConnectionString();
> > > > > > > > Console.WriteLine(connString);
>
> > > > > > > > // Use Fluent NHibernate instead of an NHibernate XML
> > > > > > > > config file
> > > > > > > > return Fluently.Configure()
> > > > > > > > .Database(MsSqlConfiguration.MsSql2005
> > > > > > > > .ConnectionString(c => c.Is(connString))
> > > > > > > > .ShowSql()
> > > > > > > > .DefaultSchema("dbo")
> > > > > > > > )
> > > > > > > > .Mappings(m => m
> > > > > > > > .FluentMappings.AddFromAssemblyOf<Program>()
> > > > > > > > )
> > > > > > > > //WARNING: will DROP/CREATE tables
> > > > .ExposeConfiguration
> > > > > > > > (BuildSchema)
> > > > > > > > .BuildSessionFactory();
> > > > > > > > }
>
> > > > > > > > I built the database myself (so I'm a control freak, sue me)
> > > > > > > > rather
> > > > > > > > than end up with weird PK and FK constraint names.
>
> > > > > > > > The DDL for the Employee table looks like this:
>
> > > > > > > > CREATE TABLE [dbo].[Employee](
> > > > > > > > [Id] [int] IDENTITY(1,1) NOT NULL,
> > > > > > > > [LastName] [nvarchar](100) NOT NULL,
> > > > > > > > [FirstName] [nvarchar](100) NOT NULL,
> > > > > > > > [Store_id] [int] NULL,
> > > > > > > > CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
> > > > > > > > (
> > > > > > > > [Id] ASC
> > > > > > > > )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
> > > > IGNORE_DUP_KEY
> > > > > > > > = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON
> > > > > > > > [PRIMARY]
> > > > > > > > ) ON [PRIMARY]
>
> > > > > > > > GO
>
> > > > > > > > ALTER TABLE [dbo].[Employee] WITH NOCHECK
> > > > > > > > ADD CONSTRAINT [FK_Employee_Store] FOREIGN KEY([Store_id])
> > > > > > > > REFERENCES [dbo].[Store] ([Id])
> > > > > > > > GO
>
> > > > > > > > ALTER TABLE [dbo].[Employee] CHECK CONSTRAINT
> > > > > > > > [FK_Employee_Store]
> > > > > > > > GO
>
> > > > > > > > The DDL for Store looks like this:
>
> > > > > > > > CREATE TABLE [dbo].[Store](
> > > > > > > > [Id] [int] IDENTITY(1,1) NOT NULL,
> > > > > > > > [Name] [nvarchar](100) NOT NULL,
> > > > > > > > CONSTRAINT [PK_Store] PRIMARY KEY CLUSTERED
> > > > > > > > (
> > > > > > > > [Id] ASC
> > > > > > > > )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
> > > > IGNORE_DUP_KEY
> > > > > > > > = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON
> > > > > > > > [PRIMARY]
> > > > > > > > ) ON [PRIMARY]
>
> > > > > > > > GO- Hide quoted text -
>
> > > > > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Fluent NHibernate" 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/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---