Answer found with CodeSmith guys. I had 2 problems:
- the files generated were a little incorrect (bag's table was not the
join table)
- and I wasn't calling Session.CommitChanges() which is the part
writing the data in the join table
Just for reference, here is the correct associated mappings.
A.hbm.xml :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="ConsoleApplication1.BusinessObjects"
assembly="ConsoleApplication1">
<class name="ConsoleApplication1.BusinessObjects.A,
ConsoleApplication1" table="[accessrights].[A]" lazy="true">
<id name="Id" column="[Aid]">
<generator class="native" />
</id>
<property name="Name" column="[name]" />
<bag name="Bs" table="[accessrights].[ABs]" lazy="true"
cascade="all" inverse="false" >
<key column="[Aid]"></key>
<many-to-many column="[Bid]" class="B" />
</bag>
</class>
</hibernate-mapping>
B.hbm.xml :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="ConsoleApplication1.BusinessObjects"
assembly="ConsoleApplication1">
<class name="ConsoleApplication1.BusinessObjects.B,
ConsoleApplication1" table="[accessrights].[B]" lazy="true">
<id name="Id" column="[Bid]">
<generator class="native" />
</id>
<property name="Name" column="[name]" />
<bag name="As" table="[accessrights].[ABs]" lazy="true"
cascade="all" inverse="false" >
<key column="[Bid]"></key>
<many-to-many column="[Aid]" class="A" />
</bag>
</class>
</hibernate-mapping>
Thanks.
On 25 jan, 18:15, Oskar Berggren <[email protected]> wrote:
> The important thing here is what the _mappings_ say, and I cannot find
> them in you mail. Those are the ones that tell NHibernate what to do.
>
> /Oskar
>
> 2010/1/25 Metasharp <[email protected]>:
>
>
>
> > as described here:http://community.codesmithtools.com/forums/t/10513.aspx
>
> > I can't get a join table ABs filled from the tables A and B
> > automatically.
>
> > Here's a copy of my previous message:
>
> > Here's my actual test sample doing this.
>
> > 1- the database
>
> > IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'accessrights')
>
> > EXEC sys.sp_executesql N'CREATE SCHEMA [accessrights] AUTHORIZATION
> > [myadminuser]'
>
> > GO
>
> > SET ANSI_NULLS ON
>
> > GO
>
> > SET QUOTED_IDENTIFIER ON
>
> > GO
>
> > IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID
> > (N'[accessrights].[B]') AND type in (N'U'))
>
> > BEGIN
>
> > CREATE TABLE [accessrights].[B](
>
> > [Bid] [int] IDENTITY(1,1) NOT NULL,
>
> > [name] [varchar](10) NULL,
>
> > CONSTRAINT [PK_B] PRIMARY KEY CLUSTERED
>
> > (
>
> > [Bid] ASC
>
> > )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
>
> > ) ON [PRIMARY]
>
> > END
>
> > GO
>
> > SET ANSI_NULLS ON
>
> > GO
>
> > SET QUOTED_IDENTIFIER ON
>
> > GO
>
> > IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID
> > (N'[accessrights].[A]') AND type in (N'U'))
>
> > BEGIN
>
> > CREATE TABLE [accessrights].[A](
>
> > [Aid] [int] IDENTITY(1,1) NOT NULL,
>
> > [name] [varchar](10) NULL,
>
> > CONSTRAINT [PK_A] PRIMARY KEY CLUSTERED
>
> > (
>
> > [Aid] ASC
>
> > )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
>
> > ) ON [PRIMARY]
>
> > END
>
> > GO
>
> > SET ANSI_NULLS ON
>
> > GO
>
> > SET QUOTED_IDENTIFIER ON
>
> > GO
>
> > IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID
> > (N'[accessrights].[ABs]') AND type in (N'U'))
>
> > BEGIN
>
> > CREATE TABLE [accessrights].[ABs](
>
> > [Aid] [int] NOT NULL,
>
> > [Bid] [int] NOT NULL
>
> > ) ON [PRIMARY]
>
> > END
>
> > GO
>
> > IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id =
> > OBJECT_ID(N'[accessrights].[FK_AB_A]') AND parent_object_id = OBJECT_ID
> > (N'[accessrights].[ABs]'))
>
> > ALTER TABLE [accessrights].[ABs] WITH CHECK ADD CONSTRAINT [FK_AB_A]
> > FOREIGN KEY([Aid])
>
> > REFERENCES [accessrights].[A] ([Aid])
>
> > GO
>
> > IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id =
> > OBJECT_ID(N'[accessrights].[FK_AB_B]') AND parent_object_id = OBJECT_ID
> > (N'[accessrights].[ABs]'))
>
> > ALTER TABLE [accessrights].[ABs] WITH CHECK ADD CONSTRAINT [FK_AB_B]
> > FOREIGN KEY([Bid])
>
> > REFERENCES [accessrights].[B] ([Bid])
>
> > 2- my hibernate.cfg.xml (set in copy always) :
>
> > <?xml version='1.0' encoding='utf-8'?>
>
> > <configuration>
>
> > <configSections>
>
> > <section name="hibernate-configuration"
> > type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
>
> > <section name="log4net"
> > type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
>
> > </configSections>
>
> > <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
>
> > <session-factory>
>
> > <property
> > name="connection.provider">NHibernate.Connection.DriverConnectionProvider</
> > property>
>
> > <property
> > name="connection.driver_class">NHibernate.Driver.SqlClientDriver</
> > property>
>
> > <property name="connection.connection_string">Data
> > Source=mydatawsource;Initial Catalog=mycatalog;Persist Security
> > Info=True;User ID=myadminuser;Password=mypassword</property>
>
> > <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</
> > property>
>
> > <property name="show_sql">false</property>
>
> > <property
> > name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFa
> > ctory,
> > NHibernate.ByteCode.Castle</property>
>
> > <property name="use_outer_join">true</property>
>
> > <mapping assembly="ConsoleApplication1" />
>
> > </session-factory>
>
> > </hibernate-configuration>
>
> > <log4net>
>
> > <appender name="ConsoleAppender"
> > type="log4net.Appender.ConsoleAppender, log4net">
>
> > <layout type="log4net.Layout.PatternLayout, log4net">
>
> > <param name="ConversionPattern" value="%m" />
>
> > </layout>
>
> > </appender>
>
> > <root>
>
> > <priority value="WARN" />
>
> > <appender-ref ref="ConsoleAppender" />
>
> > </root>
>
> > </log4net>
>
> > </configuration>
>
> > 3- the Program.cs file of my sample ConsoleApplication1 :
> > using System;
> > using ConsoleApplication1.BusinessObjects;
> > using ConsoleApplication1.ManagerObjects;
> > namespace ConsoleApplication1
> > {
> > class Program
> > {
> > static void Main(string[] args)
> > {
> > IManagerFactory mgrFactory = new ManagerFactory();
> > IAManager aMgr = mgrFactory.GetAManager();
> > IBManager bMgr = mgrFactory.GetBManager();
> > A a = new A();
> > a.Name = "a object";
> > B b = new B();
> > b.Name = "b object";
> > a.Bs.Add(b);
> > aMgr.Save(a);
> > Console.WriteLine("complete");
> > Console.ReadKey();
> > }
> > }
> > }
> > Problems..... when I save A, it saves B also.... but nothing in ABs
> > table.
>
> > Any idea?
>
> > --
> > 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.