When I Foo.Save()

INSERT INTO Foos
DEFAULT VALUES
select last_insert_rowid()

INSERT INTO Journals
           (Note,
            CreatedAt,
            CreatedBy,
            JournalableType,
            JournalableId)
VALUES     ('test note' /* @p0 */,'2009-08-07T14:55:30.00' /* @p1
*/,'jmorris' /* @p2 */,NULL /* @p3 */,NULL /* @p4 */)
select last_insert_rowid()

UPDATE Journals
SET    JournalableId = 1 /* @p0 */
WHERE  Id = 1 /* @p1 */

Now, when I Foo.Find()

SELECT foo0_.Id as Id31_0_
FROM   Foos foo0_
WHERE  foo0_.Id = 1 /* @p0 */

SELECT journals0_.JournalableId   as Journala6_1_,
       journals0_.Id              as Id1_,
       journals0_.Id              as Id24_0_,
       journals0_.Note            as Note24_0_,
       journals0_.CreatedAt       as CreatedAt24_0_,
       journals0_.CreatedBy       as CreatedBy24_0_,
       journals0_.JournalableType as Journala5_24_0_,
       journals0_.JournalableId   as Journala6_24_0_
FROM   Journals journals0_
WHERE  journals0_.JournalableId = 1 /* @p0 */

SELECT details0_.JournalId as JournalId1_,
       details0_.Id        as Id1_,
       details0_.Id        as Id1_0_,
       details0_.Key       as Key1_0_,
       details0_.OldValue  as OldValue1_0_,
       details0_.NewValue  as NewValue1_0_,
       details0_.JournalId as JournalId1_0_
FROM   JournalDetails details0_
WHERE  details0_.JournalId = 1 /* @p0 */

As I was gathering these sql statements from nhprof, I noticed the
warning, Superfluous <many-to-one> update - use inverse='true'.  So,
setting inverse = true on my HasMany in Foo didn't help, but I rewrote
my test like such

        [Test]
        public void AddJournalInverse()
        {
            var foo = new Foo();
            foo.Save();

            var journal = new JournalBuilder().DoNotPersist().Build();

            journal.Journalable = foo;

            journal.Save();

            var actual = Foo.Find(foo.Id);

            Assert.That(actual.Journals, Has.Count(1));

            foreach (var j in actual.Journals)
            {
                Assert.That(j.Journalable, Is.Not.Null);
            }
        }

So, I was saving the Journal object with the association to Foo.  My
test passed with my Journal instances having a reference to their Foo
object.

Shouldn't I be able to do Foo.Save() after adding Journals to Foo?

Jason

On Fri, Aug 7, 2009 at 7:00 PM, Markus Zywitza<[email protected]> wrote:
> Can you post the generated SQL?
>
> -Markus
>
> 2009/8/7 jsmorris <[email protected]>
>>
>> Thanks for pointing that out.  I misunderstood what MetaType was
>> referring to.  The error is gone, but another problem appeared.  I
>> tried to complete my simple test
>>
>>            var foo = new Foo();
>>
>>            foo.Journals.Add(new JournalBuilder().DoNotPersist().Build());
>>
>>            foo.Save();
>>
>>            var actual = Foo.Find(foo.Id);
>>
>>            Assert.That(actual.Journals, Has.Count(1));
>>
>>            foreach (var journal in actual.Journals)
>>            {
>>                Assert.That(journal.Journalable, Is.Not.Null);
>>            }
>>
>> The problem is that in the Journals table, the JournalableId is
>> populated with the PK of Foo, but the JournalableType is null.  So,
>> when I check my association from Journal to Foo it is null.  Ideas?
>>
>> Again, thanks for the help Markus.
>>
>> Jason
>>
>>
>> On Fri, Aug 7, 2009 at 7:32 AM, Markus Zywitza<[email protected]>
>> wrote:
>> > Your meta-type is string ("Foo","Bar"), so you have to specify
>> > MetaType=typeof(string).
>> > see here:
>> >
>> > http://www.castleproject.org/activerecord/documentation/trunk/usersguide/relations/hasmanytoany.html
>> >
>> > -Markus
>> >
>> > 2009/8/6 jsmorris <[email protected]>
>> >>
>> >> Here are the hbm files that are generated when I include the [Any]
>> >> attribute on the Journal.Journalable property.
>> >>
>> >> On Wed, Aug 5, 2009 at 9:46 PM, Markus
>> >> Zywitza<[email protected]>
>> >> wrote:
>> >> > Can you send in the generated hbm files? You'll have to specify
>> >> > debug="true"
>> >> > in your config for them to be written to disk.
>> >> >
>> >> > -Markus
>> >> >
>> >> > 2009/8/5 jsmorris <[email protected]>
>> >> >>
>> >> >> Thanks for your continued help Markus.  I now understand what you
>> >> >> are
>> >> >> saying about the HasMany side of this relationship and was able to
>> >> >> make it work!  But, now the BelongsTo/Any side of the relationship
>> >> >> is
>> >> >> throwing an error.  I attempted to use this mapping on my Journal
>> >> >> object to reference which object this Journal belongs to (either
>> >> >> Foo,
>> >> >> Bar, or any other IJournalable object).
>> >> >>
>> >> >>        [Any(IdType = typeof(Int32), MetaType = typeof(IJournalable),
>> >> >> TypeColumn = "JournalableType", IdColumn = "JournalableId", Cascade
>> >> >> =
>> >> >> CascadeEnum.SaveUpdate)]
>> >> >>        [Any.MetaValue("Foo", typeof(Foo))]
>> >> >>        [Any.MetaValue("Bar", typeof(Bar))]
>> >> >>        public IJournalable Journalable { get; set; }
>> >> >>
>> >> >> But, got the below errors.  I am not sure, even reading the inner
>> >> >> exception, that I know where to start looking.
>> >> >>
>> >> >> Jason
>> >> >>
>> >> >> System.NullReferenceException: Object reference not set to an
>> >> >> instance
>> >> >> of an object.
>> >> >> at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindAny(XmlNode node,
>> >> >> Any
>> >> >> model, Boolean isNullable)
>> >> >> at
>> >> >> NHibernate.Cfg.XmlHbmBinding.ClassBinder.PropertiesFromXML(XmlNode
>> >> >> node, PersistentClass model, IDictionary`2 inheritedMetas, UniqueKey
>> >> >> uniqueKey, Boolean mutable, Boolean nullable, Boolean naturalId)
>> >> >> at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(XmlNode node,
>> >> >> HbmClass classSchema, IDictionary`2 inheritedMetas)
>> >> >> at
>> >> >>
>> >> >> NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(XmlNode
>> >> >> parentNode, IDictionary`2 inheritedMetas)
>> >> >> at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(XmlNode node)
>> >> >> at
>> >> >> NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument
>> >> >> doc)
>> >> >> NHibernate.MappingException: Could not compile the mapping document:
>> >> >> (string)
>> >> >> at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
>> >> >> at
>> >> >> NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument
>> >> >> doc)
>> >> >> at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
>> >> >> at
>> >> >>
>> >> >> NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument
>> >> >> document)
>> >> >> at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader,
>> >> >> String
>> >> >> name)
>> >> >> at NHibernate.Cfg.Configuration.AddXml(String xml, String name)
>> >> >> at NHibernate.Cfg.Configuration.AddXmlString(String xml)
>> >> >> at
>> >> >> Castle.ActiveRecord.ActiveRecordStarter.AddXmlString(Configuration
>> >> >> config, String xml, ActiveRecordModel model) in
>> >> >>
>> >> >>
>> >> >>
>> >> >> c:\TeamCity\buildAgent\work\7e4107d1782544ec\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordStarter.cs:
>> >> >> line 761
>> >> >> Castle.ActiveRecord.Framework.ActiveRecordException: Error adding
>> >> >> information from class Intel.TraX.Tests.Data.Store.Journal to
>> >> >> NHibernate. Check the inner exception for more information
>> >> >> at
>> >> >> Castle.ActiveRecord.ActiveRecordStarter.AddXmlString(Configuration
>> >> >> config, String xml, ActiveRecordModel model) in
>> >> >>
>> >> >>
>> >> >>
>> >> >> c:\TeamCity\buildAgent\work\7e4107d1782544ec\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordStarter.cs:
>> >> >> line 765
>> >> >> at
>> >> >>
>> >> >>
>> >> >> Castle.ActiveRecord.ActiveRecordStarter.AddXmlToNHibernateCfg(ISessionFactoryHolder
>> >> >> holder, ActiveRecordModelCollection models) in
>> >> >>
>> >> >>
>> >> >>
>> >> >> c:\TeamCity\buildAgent\work\7e4107d1782544ec\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordStarter.cs:
>> >> >> line 751
>> >> >> at
>> >> >>
>> >> >>
>> >> >> Castle.ActiveRecord.ActiveRecordStarter.RegisterTypes(ISessionFactoryHolder
>> >> >> holder, IConfigurationSource source, IEnumerable`1 types, Boolean
>> >> >> ignoreProblematicTypes) in
>> >> >>
>> >> >>
>> >> >>
>> >> >> c:\TeamCity\buildAgent\work\7e4107d1782544ec\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordStarter.cs:
>> >> >> line 933
>> >> >> at
>> >> >>
>> >> >> Castle.ActiveRecord.ActiveRecordStarter.Initialize(IConfigurationSource
>> >> >> source, Type[] types) in
>> >> >>
>> >> >>
>> >> >>
>> >> >> c:\TeamCity\buildAgent\work\7e4107d1782544ec\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordStarter.cs:
>> >> >> line 200
>> >> >> at Castle.ActiveRecord.ActiveRecordStarter.Initialize(Assembly[]
>> >> >> assemblies, IConfigurationSource source, Type[] additionalTypes) in
>> >> >>
>> >> >>
>> >> >>
>> >> >> c:\TeamCity\buildAgent\work\7e4107d1782544ec\ActiveRecord\Castle.ActiveRecord\Framework\ActiveRecordStarter.cs:
>> >> >> line 223
>> >> >> at
>> >> >>
>> >> >>
>> >> >> Castle.Facilities.ActiveRecordIntegration.ActiveRecordFacility.InitializeFramework(ArrayList
>> >> >> assemblies) in
>> >> >>
>> >> >>
>> >> >> c:\TeamCity\buildAgent\work\7e4107d1782544ec\Facilities\ActiveRecordIntegration\Castle.Facilities.ActiveRecordIntegration\ActiveRecordFacility.cs:
>> >> >> line 147
>> >> >> at
>> >> >>
>> >> >> Castle.Facilities.ActiveRecordIntegration.ActiveRecordFacility.Init()
>> >> >> in
>> >> >>
>> >> >>
>> >> >> c:\TeamCity\buildAgent\work\7e4107d1782544ec\Facilities\ActiveRecordIntegration\Castle.Facilities.ActiveRecordIntegration\ActiveRecordFacility.cs:
>> >> >> line 107
>> >> >> at Castle.MicroKernel.Facilities.AbstractFacility.Init(IKernel
>> >> >> kernel,
>> >> >> IConfiguration facilityConfig) in
>> >> >>
>> >> >>
>> >> >>
>> >> >> c:\TeamCity\buildAgent\work\7e4107d1782544ec\InversionOfControl\Castle.MicroKernel\Facilities\AbstractFacility.cs:
>> >> >> line 69
>> >> >> at Castle.MicroKernel.DefaultKernel.AddFacility(String key,
>> >> >> IFacility
>> >> >> facility) in
>> >> >>
>> >> >>
>> >> >> c:\TeamCity\buildAgent\work\7e4107d1782544ec\InversionOfControl\Castle.MicroKernel\DefaultKernel.cs:
>> >> >> line 515
>> >> >> at Castle.Windsor.WindsorContainer.AddFacility(String key, IFacility
>> >> >> facility) in
>> >> >>
>> >> >>
>> >> >> c:\TeamCity\buildAgent\work\7e4107d1782544ec\InversionOfControl\Castle.Windsor\WindsorContainer.cs:
>> >> >> line 261
>> >> >> at Intel.TraX.Web.IoCContainer.RegisterFacilities() in
>> >> >> IoCContainer.cs:
>> >> >> line 57
>> >> >> at Intel.TraX.Web.IoCContainer..ctor(String configFileName) in
>> >> >> IoCContainer.cs: line 34
>> >> >> at Intel.TraX.Tests.BasePersistenceTestFixture.TestFixtureSetUp() in
>> >> >> BasePersistenceTestFixture.cs: line 37
>> >> >>
>> >> >>
>> >> >>
>> >> >> On Wed, Aug 5, 2009 at 10:18 AM, Markus
>> >> >> Zywitza<[email protected]>
>> >> >> wrote:
>> >> >> > Maybe I was unclear. BelongsTo won't help here, Any is the reight
>> >> >> > attribute.
>> >> >> > You have to specify
>> >> >> >
>> >> >> > HasMany(typeof(Journal), ColumnKey="...", Table="...")
>> >> >> >
>> >> >> > This should work.
>> >> >> > -Markus
>> >> >> > 2009/8/4 jsmorris <[email protected]>
>> >> >> >>
>> >> >> >> Moved from dev list...my apologies for asking my question on the
>> >> >> >> wrong
>> >> >> >> list.
>> >> >> >>
>> >> >> >> I now understand that I need the BelongsTo, but my problem is
>> >> >> >> constructing the correct BelongsTo in this situation.  All my
>> >> >> >> attempts
>> >> >> >> have failed.
>> >> >> >>
>> >> >> >> So, my Journal class needs a BelongsTo to associate it to either
>> >> >> >> a
>> >> >> >> Foo
>> >> >> >> or Bar, so if I define this
>> >> >> >>
>> >> >> >>  [BelongsTo("JournalableId", Table = "Journals")]
>> >> >> >>  public IJournalable Journalable { get; set; }
>> >> >> >>
>> >> >> >> I get the the error message.  If I type the property to either of
>> >> >> >> my
>> >> >> >> concrete types Foo or Bar, I do not get an error.
>> >> >> >>
>> >> >> >>  [BelongsTo("JournalableId", Table = "Journals")]
>> >> >> >>  public Foo Journalable { get; set; }
>> >> >> >>
>> >> >> >> or
>> >> >> >>
>> >> >> >>  [BelongsTo("JournalableId", Table = "Journals")]
>> >> >> >>  public Bar Journalable { get; set; }
>> >> >> >>
>> >> >> >> I feel that I need to be able to specify a type to the BelongsTo.
>> >> >> >>  I
>> >> >> >> was attempting to use the Any attribute to specify my metatype,
>> >> >> >> IJournalable, as well as the concrete types, Foo and Bar,  for
>> >> >> >> this
>> >> >> >> association.  I believe that my Journals table would end up with
>> >> >> >> columns Id, JournalableId, JournalableType where JournalableType
>> >> >> >> would
>> >> >> >> be either Foo or Bar.
>> >> >> >>
>> >> >> >> Is this type of association even possible?
>> >> >> >>
>> >> >> >> Thanks,
>> >> >> >> Jason
>> >> >> >>
>> >> >> >>
>> >> >> >>
>> >> >> >> On Fri, Jul 31, 2009 at 7:34 PM, Markus
>> >> >> >> Zywitza<[email protected]>
>> >> >> >> wrote:
>> >> >> >> > Hint: It's already explained in the exception: If you don't
>> >> >> >> > have a
>> >> >> >> > BelongsTo
>> >> >> >> > (Any doesn't count), you need to specify ColumnKey and Table.
>> >> >> >> >
>> >> >> >> > -Markus
>> >> >> >> >
>> >> >> >> > 2009/7/31 jsmorris <[email protected]>
>> >> >> >> >>
>> >> >> >> >> I am having trouble setting up my AR relationships.  I have
>> >> >> >> >> tried
>> >> >> >> >> so
>> >> >> >> >> many things, but nothing seems to work.  Here is what I am
>> >> >> >> >> trying
>> >> >> >> >> to
>> >> >> >> >> do.
>> >> >> >> >>
>> >> >> >> >> Foo has many Journals.  Bar has many Journals. A Journal can
>> >> >> >> >> belong
>> >> >> >> >> to
>> >> >> >> >> either Foo or Bar.
>> >> >> >> >>
>> >> >> >> >> If I try the mapping I have below, I get the following error
>> >> >> >> >>
>> >> >> >> >> Castle.ActiveRecord.Framework.ActiveRecordException:
>> >> >> >> >> ActiveRecord
>> >> >> >> >> tried to infer details about the relation Bar.Journals but it
>> >> >> >> >> could
>> >> >> >> >> not find a 'BelongsTo' mapped property in the target type
>> >> >> >> >> Intel.TraX.Tests.Data.Store.Journal
>> >> >> >> >>
>> >> >> >> >> I am probably way off on what needs to be done, but I am
>> >> >> >> >> looking
>> >> >> >> >> for
>> >> >> >> >> any help to point me in the right direction.
>> >> >> >> >>
>> >> >> >> >> Thanks,
>> >> >> >> >> Jason
>> >> >> >> >>
>> >> >> >> >>    [ActiveRecord]
>> >> >> >> >>    public class Journal : DomainObject<Journal>
>> >> >> >> >>    {
>> >> >> >> >>        [Any(typeof(Int32), MetaType = typeof(IJournalable),
>> >> >> >> >> TypeColumn = "JournalableType", IdColumn = "JournalableId",
>> >> >> >> >> Cascade
>> >> >> >> >> =
>> >> >> >> >> CascadeEnum.SaveUpdate)]
>> >> >> >> >>        [Any.MetaValue("Foo", typeof(Foo))]
>> >> >> >> >>        [Any.MetaValue("Bar", typeof(Bar))]
>> >> >> >> >>        public IJournalable Journalable { get; set; }
>> >> >> >> >>    }
>> >> >> >> >>
>> >> >> >> >>    public interface IJournalable {}
>> >> >> >> >>
>> >> >> >> >>    [ActiveRecord]
>> >> >> >> >>    public class Foo : DomainObject<Foo>, IJournalable
>> >> >> >> >>    {
>> >> >> >> >>        [HasMany(ColumnKey = "JournalableId")]
>> >> >> >> >>        public IList<Journal> Journals { get; set; }
>> >> >> >> >>    }
>> >> >> >> >>
>> >> >> >> >>    [ActiveRecord]
>> >> >> >> >>    public class Bar : DomainObject<Bar>, IJournalable
>> >> >> >> >>    {
>> >> >> >> >>        [HasMany(ColumnKey = "JournalableId")]
>> >> >> >> >>        public IList<Journal> Journals { get; set; }
>> >> >> >> >>    }
>> >> >> >> >> }
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >
>> >> >> >> >
>> >> >> >> > >
>> >> >> >> >
>> >> >> >> >>
>> >> >> >
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >> > >
>> >> >
>> >>
>> >>
>> >
>> >
>> > >
>> >
>>
>>
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" 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/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to