コード例 #1
0
        protected override void OnSetUp()
        {
            using (var session = OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    var child1 = new EntitySimpleChild
                    {
                        Name = "Child1"
                    };
                    var child2 = new EntitySimpleChild
                    {
                        Name = "Child1"
                    };

                    var parent = new EntityComplex
                    {
                        Name          = "ComplexEnityParent",
                        Child1        = child1,
                        Child2        = child2,
                        LazyProp      = "SomeBigValue",
                        SameTypeChild = new EntityComplex()
                        {
                            Name = "ComplexEntityChild"
                        }
                    };

                    _entityWithCustomEntityName = new EntityCustomEntityName()
                    {
                        Name = "EntityCustomEntityName"
                    };

                    _entityWithCompositeId = new EntityWithCompositeId
                    {
                        Key = new CompositeKey
                        {
                            Id1 = 1,
                            Id2 = 2
                        },
                        Name = "Composite"
                    };

                    session.Save(child1);
                    session.Save(child2);
                    session.Save(parent.SameTypeChild);
                    session.Save(parent);
                    session.Save(_entityWithCompositeId);
                    session.Save(customEntityName, _entityWithCustomEntityName);

                    session.Flush();
                    transaction.Commit();
                }
        }
コード例 #2
0
        public void EntityJoinForCompositeKey()
        {
            using (var sqlLog = new SqlLogSpy())
                using (var session = OpenSession())
                {
                    EntityWithCompositeId   ejComposite = null;
                    EntityWithNoAssociation root        = null;
                    root = session
                           .QueryOver(() => root)
                           .JoinEntityAlias(() => ejComposite, () => root.Composite1Key1 == ejComposite.Key.Id1 && root.Composite1Key2 == ejComposite.Key.Id2)
                           .Take(1).SingleOrDefault();
                    var composite = session.Load <EntityWithCompositeId>(_entityWithCompositeId.Key);

                    Assert.That(NHibernateUtil.IsInitialized(composite), Is.True, "Object must be initialized");
                    Assert.That(composite, Is.EqualTo(_entityWithCompositeId).Using((EntityWithCompositeId x, EntityWithCompositeId y) => (Equals(x.Key, y.Key) && Equals(x.Name, y.Name)) ? 0 : 1));
                    Assert.That(sqlLog.Appender.GetEvents().Length, Is.EqualTo(1), "Only one SQL select is expected");
                }
        }