|  | 
|  | 1 | +using System.Linq; | 
|  | 2 | +using NHibernate.Cfg.MappingSchema; | 
|  | 3 | +using NHibernate.DomainModel; | 
|  | 4 | +using NHibernate.Mapping.ByCode; | 
|  | 5 | +using NUnit.Framework; | 
|  | 6 | + | 
|  | 7 | +namespace NHibernate.Test.NHSpecificTest.GH3363 | 
|  | 8 | +{ | 
|  | 9 | +	/// <summary> | 
|  | 10 | +	/// Fixture using 'by code' mappings | 
|  | 11 | +	/// </summary> | 
|  | 12 | +	/// <remarks> | 
|  | 13 | +	/// This fixture is identical to <see cref="Fixture" /> except the <see cref="Entities" /> mapping is performed  | 
|  | 14 | +	/// by code in the GetMappings method, and does not require the <c>Mappings.hbm.xml</c> file. Use this approach | 
|  | 15 | +	/// if you prefer. | 
|  | 16 | +	/// </remarks> | 
|  | 17 | +	[TestFixture] | 
|  | 18 | +	public class ByCodeFixture : TestCaseMappingByCode | 
|  | 19 | +	{ | 
|  | 20 | +		protected override HbmMapping GetMappings() | 
|  | 21 | +		{ | 
|  | 22 | +			var mapper = new ModelMapper(); | 
|  | 23 | +		 | 
|  | 24 | +			mapper.Class<Mother>(rc => | 
|  | 25 | +			{ | 
|  | 26 | +				rc.Id(x => x.Id, m => m.Generator(Generators.Identity)); | 
|  | 27 | +				rc.Property(x => x.Name); | 
|  | 28 | +				rc.Discriminator(x => x.Column("kind")); | 
|  | 29 | +			}); | 
|  | 30 | +			mapper.Subclass<Child1>(rc => | 
|  | 31 | +			{ | 
|  | 32 | +				rc.Property(x => x.Name); | 
|  | 33 | +				rc.ManyToOne(x => x.Thing,  m =>{ | 
|  | 34 | +						m.NotFound(NotFoundMode.Ignore); | 
|  | 35 | +						m.Column("thingId"); | 
|  | 36 | +					}); | 
|  | 37 | +				rc.DiscriminatorValue(1); | 
|  | 38 | +			}); | 
|  | 39 | +			mapper.Subclass<Child2>(rc => | 
|  | 40 | +			{ | 
|  | 41 | +				rc.Property(x => x.Name); | 
|  | 42 | +				rc.ManyToOne(x => x.Thing, m => { | 
|  | 43 | +					m.NotFound(NotFoundMode.Ignore); | 
|  | 44 | +					m.Column("thingId"); | 
|  | 45 | +				}); | 
|  | 46 | +				rc.DiscriminatorValue(2); | 
|  | 47 | +			}); | 
|  | 48 | +			mapper.Class<Thing1>(rc => | 
|  | 49 | +			{ | 
|  | 50 | +				rc.Id(x => x.Id, m => m.Generator(Generators.Assigned)); | 
|  | 51 | +				rc.Property(x => x.Name); | 
|  | 52 | +				 | 
|  | 53 | +			}); | 
|  | 54 | +			mapper.Class<Thing2>(rc => | 
|  | 55 | +			{ | 
|  | 56 | +				rc.Id(x => x.Id, m => m.Generator(Generators.Assigned)); | 
|  | 57 | +				rc.Property(x => x.Name); | 
|  | 58 | + | 
|  | 59 | +			}); | 
|  | 60 | +			return mapper.CompileMappingForAllExplicitlyAddedEntities(); | 
|  | 61 | +		} | 
|  | 62 | + | 
|  | 63 | +		protected override void OnSetUp() | 
|  | 64 | +		{ | 
|  | 65 | +			using (var session = OpenSession()) | 
|  | 66 | +			using (var transaction = session.BeginTransaction()) | 
|  | 67 | +			{ | 
|  | 68 | +				var t1 = new Thing1() { Name = "don't care",Id="00001" }; | 
|  | 69 | +				session.Save(t1); | 
|  | 70 | +				var t2 = new Thing2() { Name = "look for this",Id="00002" }; | 
|  | 71 | +				session.Save(t2); | 
|  | 72 | +				var child1 = new Child1 { Name = "Child1",Thing=t1 }; | 
|  | 73 | +				session.Save(child1); | 
|  | 74 | +				var child2 = new Child2 { Name = "Child1", Thing = t2 }; | 
|  | 75 | +				session.Save(child2); | 
|  | 76 | +				transaction.Commit(); | 
|  | 77 | +			} | 
|  | 78 | +		} | 
|  | 79 | + | 
|  | 80 | +		protected override void OnTearDown() | 
|  | 81 | +		{ | 
|  | 82 | +			using (var session = OpenSession()) | 
|  | 83 | +			using (var transaction = session.BeginTransaction()) | 
|  | 84 | +			{ | 
|  | 85 | +				// The HQL delete does all the job inside the database without loading the entities, but it does | 
|  | 86 | +				// not handle delete order for avoiding violating constraints if any. Use | 
|  | 87 | +				// session.Delete("from System.Object"); | 
|  | 88 | +				// instead if in need of having NHbernate ordering the deletes, but this will cause | 
|  | 89 | +				// loading the entities in the session. | 
|  | 90 | +				session.CreateQuery("delete from System.Object").ExecuteUpdate(); | 
|  | 91 | + | 
|  | 92 | +				transaction.Commit(); | 
|  | 93 | +			} | 
|  | 94 | +		} | 
|  | 95 | + | 
|  | 96 | +		[Test] | 
|  | 97 | +		public void LookForThingOfTypeThing1() | 
|  | 98 | +		{ | 
|  | 99 | +			using (var session = OpenSession()) | 
|  | 100 | +			using (var transaction = session.BeginTransaction()) | 
|  | 101 | +			{ | 
|  | 102 | +				/* | 
|  | 103 | +				 * wrong statement created | 
|  | 104 | +				 * select mother0_.Id as id1_0_, mother0_.Name as name3_0_, | 
|  | 105 | +					mother0_.thingId as thingid4_0_, mother0_.kind as kind2_0_ from Mother mother0_  | 
|  | 106 | +					left outer join Thing2 thing2x1_ on  | 
|  | 107 | +					mother0_.thingId=thing2x1_.Id where mother0_.kind='1' and thing2x1_.Id=?" | 
|  | 108 | +				 *  | 
|  | 109 | +				 */ | 
|  | 110 | + | 
|  | 111 | +				var result = session.Query<Mother>().Where(k => k is Child1 && (k as Child1).Thing.Id == "00001").ToList(); | 
|  | 112 | + | 
|  | 113 | +				Assert.That(result, Has.Count.EqualTo(1)); | 
|  | 114 | +				transaction.Commit(); | 
|  | 115 | +			} | 
|  | 116 | +		} | 
|  | 117 | +	} | 
|  | 118 | + | 
|  | 119 | +} | 
0 commit comments