【发布时间】:2011-08-03 11:54:25
【问题描述】:
我正在尝试设置以针对我们的 oracle 数据库使用 nhibernate。 我的目标之一是测试查询逻辑。我开始使用 sqlite,但遇到了多对一关系的问题,没有引入外来对象。认为这是一个 sqlite 问题,我设置了一个新的 oracle 数据库进行测试,但我得到了相同的症状。
当我对填充的数据库运行查询时,它可以正常工作并返回正确延迟加载外部对象的结果,因为它们在代码中被引用。
查询逻辑测试(包含在 mstest 程序集中)返回空值而不是外来对象。
两个查询的区别:
1. The test query creates,saves and flushes the data just before
calling the query.
2. The test configuration includes steps needed to export the mapped schema
into test database, uses a different default schema and connection string.
3. The test query happens in a mstest unit test assembly.
我已经确认:
1. That running query against real database works even when run from the unit test
assembly.
2. That the test data is being save in the test database schema.
3. That the foreign key reference is created in the test database.
4. That the primary keys are being created in both tables used in query.
我怀疑我在将数据保存到测试数据库时错过了一个步骤,所以我将分享该代码,如果需要,我可以添加配置代码。
var session = DbSession.Load(ConnModes.UnitTest);
var uil = new List<UserInfo>();
uil.Add(new UserInfo { Id = -1, FullName = "Fred Flintstone", LoginName = "fredflint" });
uil.Add(new UserInfo { Id = -1, FullName = "Barney Ruble", LoginName = "barnrubl" });
uil.Add(new UserInfo { Id = -1, FullName = "Bam Bam", LoginName = "bambam" });
foreach (var f in uil)
{
session.Save(f);
}
var rightNow = DateTime.Now;
var x = new List<FacilityRouteFormula>();
x.Add(new FacilityRouteFormula { Id = -1, TrackingFacilityId = 2, Formula = "a formula1", ComponentGroupName = "Comp 0", UserId = 1, CreationDate = rightNow.AddMinutes(5) });
x.Add(new FacilityRouteFormula { Id = -1, TrackingFacilityId = 2, Formula = "a formula2", ComponentGroupName = "Comp 0", UserId = 1, CreationDate = rightNow.AddMinutes(3) });
x.Add(new FacilityRouteFormula { Id = -1, TrackingFacilityId = 2, Formula = "a formula3", ComponentGroupName = "Comp 0", UserId = 1, CreationDate = rightNow.AddMinutes(4) });
x.Add(new FacilityRouteFormula { Id = -1, TrackingFacilityId = 2, Formula = "a formula4", ComponentGroupName = "Comp 0", UserId = 1, CreationDate = rightNow.AddDays(1) });
x.Add(new FacilityRouteFormula { Id = -1, TrackingFacilityId = 2, Formula = "a formula5", ComponentGroupName = "Comp 0", UserId = 1, CreationDate = rightNow });
foreach (var f in x)
{
session.Save(f);
}
session.Flush();
var uidata2 = (from uid in session.Query<UserInfo>() select uid).ToList();
var data = (from uid in session.Query<FacilityRouteFormula>() select uid).ToList();
var q = (from frf in session.Query<FacilityRouteFormula>()
where frf.ComponentGroupName == "Comp 0" &&
frf.TrackingFacilityId == 2
orderby frf.CreationDate descending
select frf).ToList();
Assert.IsNotNull(q[0].User);
Assert.AreEqual("fredflint", q[0].User.LoginName);
这是针对“真实”数据库的查询。
var s = DbSession.Load(ConnModes.Dev );
var q = (from frf in s.Query<FacilityRouteFormula>()
where frf.ComponentGroupName == "Comp 0" &&
frf.TrackingFacilityId == 2
orderby frf.CreationDate descending
select frf).ToList();
按要求映射文件:
设施路线公式:
<id name="Id" column="N_FACILITY_ROUTE_FORMULA_ID" type="long" unsaved-value="-1">
<generator class="native" >
<param name="sequence">SEQ_FACILITY_ROUTE_FORMULA</param>
</generator>
</id>
<property name="TrackingFacilityId" column="N_TRACKING_FACILITY_ID" type="long" />
<property name ="ComponentGroupName" column="C_COMPONENT_GROUP_NAME" type="string"/>
<property name="CreationDate" column="D_CREATION_DATE" type="DateTime"/>
<property name="UserId" column="N_USER_ID" type="int" not-null="true" />
<property name="Formula" column="C_FORMULA" type="string" not-null="true" />
<many-to-one insert="false" update="false" lazy="false" name="User" fetch="select">
<column name="N_USER_ID" sql-type="NUMBER" not-null="true" />
</many-to-one>
用户信息:
<id name="Id" column="N_USER_ID" type="int" unsaved-value="-1" >
<generator class="native" >
<param name="sequence">SEQ_USER_ID</param>
</generator>
</id>
<property name="FullName" column="C_USER_FULL_NAME" type="string" not-null="true" />
<property name="LoginName" column="C_LOGIN_NAME" type="string" not-null="true" />
【问题讨论】:
-
对不起,先生,我在这里没有看到问题
-
问题是为什么在针对测试数据库而不是“真实”数据库运行查询时,外键对象不延迟加载。
-
您在哪里将用户分配给
FacilityRouteFormula? UserId 只是一个 int 但没有用户引用。我希望FacilityRouteFormula { User = uil[0] }或类似的东西。也许在 FacilityRouteFormula 映射中发布与用户的关系映射会有所帮助。 -
在查询测试中,我设置了 UserId = 1。由于这是一个 ORM,我希望 User 对象能够延迟加载,就像在访问非查询测试数据库时一样。
标签: nhibernate