Skip to content

Commit f7dc55f

Browse files
bahusoidfredericDelaporte
authored andcommitted
Fixed entity name retrieval for EntityProjection (#1534)
1 parent bea25bf commit f7dc55f

File tree

4 files changed

+91
-11
lines changed

4 files changed

+91
-11
lines changed

src/NHibernate.Test/Async/Criteria/EntityProjectionsTest.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ namespace NHibernate.Test.Criteria
2525
[TestFixture]
2626
public class EntityProjectionsTestAsync : TestCaseMappingByCode
2727
{
28+
private const string customEntityName = "CustomEntityName";
2829
private EntityWithCompositeId _entityWithCompositeId;
30+
private EntityCustomEntityName _entityWithCustomEntityName;
2931

3032
protected override HbmMapping GetMappings()
3133
{
@@ -76,6 +78,16 @@ protected override HbmMapping GetMappings()
7678

7779
rc.Property(e => e.Name);
7880
});
81+
82+
mapper.Class<EntityCustomEntityName>(
83+
rc =>
84+
{
85+
rc.EntityName(customEntityName);
86+
87+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
88+
rc.Property(x => x.Name);
89+
});
90+
7991
return mapper.CompileMappingForAllExplicitlyAddedEntities();
8092
}
8193

@@ -117,6 +129,11 @@ protected override void OnSetUp()
117129
}
118130
};
119131

132+
_entityWithCustomEntityName = new EntityCustomEntityName()
133+
{
134+
Name = "EntityCustomEntityName"
135+
};
136+
120137
_entityWithCompositeId = new EntityWithCompositeId
121138
{
122139
Key = new CompositeKey
@@ -132,6 +149,7 @@ protected override void OnSetUp()
132149
session.Save(parent.SameTypeChild);
133150
session.Save(parent);
134151
session.Save(_entityWithCompositeId);
152+
session.Save(customEntityName, _entityWithCustomEntityName);
135153

136154
session.Flush();
137155
transaction.Commit();
@@ -459,5 +477,22 @@ public async Task EntityProjectionForCompositeKeyLazyAsync()
459477
Assert.That(NHibernateUtil.IsInitialized(composite), Is.False, "Object must be lazy loaded.");
460478
}
461479
}
480+
481+
[Test]
482+
public async Task EntityProjectionForCustomEntityNameAsync()
483+
{
484+
using (var session = OpenSession())
485+
{
486+
var entity = await (session
487+
.QueryOver<EntityCustomEntityName>(customEntityName)
488+
.Select(Projections.RootEntity())
489+
.Take(1).SingleOrDefaultAsync());
490+
491+
Assert.That(entity, Is.Not.Null);
492+
Assert.That(NHibernateUtil.IsInitialized(entity), Is.True, "Object must be initialized.");
493+
Assert.That(entity.Id, Is.EqualTo(_entityWithCustomEntityName.Id));
494+
Assert.That(entity.Name, Is.EqualTo(_entityWithCustomEntityName.Name));
495+
}
496+
}
462497
}
463498
}

src/NHibernate.Test/Criteria/EntityProjectionsEntities.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ public class EntitySimpleChild
88
public virtual Guid Id { get; set; }
99
public virtual string Name { get; set; }
1010
}
11+
12+
public class EntityCustomEntityName
13+
{
14+
public virtual Guid Id { get; set; }
15+
public virtual string Name { get; set; }
16+
}
1117

1218
public class EntityComplex
1319
{

src/NHibernate.Test/Criteria/EntityProjectionsTest.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ namespace NHibernate.Test.Criteria
1414
[TestFixture]
1515
public class EntityProjectionsTest : TestCaseMappingByCode
1616
{
17+
private const string customEntityName = "CustomEntityName";
1718
private EntityWithCompositeId _entityWithCompositeId;
19+
private EntityCustomEntityName _entityWithCustomEntityName;
1820

1921
protected override HbmMapping GetMappings()
2022
{
@@ -65,6 +67,16 @@ protected override HbmMapping GetMappings()
6567

6668
rc.Property(e => e.Name);
6769
});
70+
71+
mapper.Class<EntityCustomEntityName>(
72+
rc =>
73+
{
74+
rc.EntityName(customEntityName);
75+
76+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
77+
rc.Property(x => x.Name);
78+
});
79+
6880
return mapper.CompileMappingForAllExplicitlyAddedEntities();
6981
}
7082

@@ -106,6 +118,11 @@ protected override void OnSetUp()
106118
}
107119
};
108120

121+
_entityWithCustomEntityName = new EntityCustomEntityName()
122+
{
123+
Name = "EntityCustomEntityName"
124+
};
125+
109126
_entityWithCompositeId = new EntityWithCompositeId
110127
{
111128
Key = new CompositeKey
@@ -121,6 +138,7 @@ protected override void OnSetUp()
121138
session.Save(parent.SameTypeChild);
122139
session.Save(parent);
123140
session.Save(_entityWithCompositeId);
141+
session.Save(customEntityName, _entityWithCustomEntityName);
124142

125143
session.Flush();
126144
transaction.Commit();
@@ -448,5 +466,22 @@ public void EntityProjectionForCompositeKeyLazy()
448466
Assert.That(NHibernateUtil.IsInitialized(composite), Is.False, "Object must be lazy loaded.");
449467
}
450468
}
469+
470+
[Test]
471+
public void EntityProjectionForCustomEntityName()
472+
{
473+
using (var session = OpenSession())
474+
{
475+
var entity = session
476+
.QueryOver<EntityCustomEntityName>(customEntityName)
477+
.Select(Projections.RootEntity())
478+
.Take(1).SingleOrDefault();
479+
480+
Assert.That(entity, Is.Not.Null);
481+
Assert.That(NHibernateUtil.IsInitialized(entity), Is.True, "Object must be initialized.");
482+
Assert.That(entity.Id, Is.EqualTo(_entityWithCustomEntityName.Id));
483+
Assert.That(entity.Name, Is.EqualTo(_entityWithCustomEntityName.Name));
484+
}
485+
}
451486
}
452487
}

src/NHibernate/Criterion/EntityProjection.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,23 +151,27 @@ private void SetFields(ICriteriaQuery criteriaQuery)
151151
entityProjectionQuery.RegisterEntityProjection(this);
152152
}
153153

154-
if (_entityType == null)
155-
{
156-
_entityType = criteria.GetRootEntityTypeIfAvailable();
157-
}
158-
159154
if (_entityAlias == null)
160155
{
161156
_entityAlias = criteria.Alias;
162157
}
163158

164-
Persister = criteriaQuery.Factory.GetEntityPersister(_entityType.FullName) as IQueryable;
165-
if (Persister == null)
166-
throw new HibernateException($"Projecting to entities requires a '{typeof(IQueryable).FullName}' persister, '{_entityType.FullName}' does not have one.");
159+
ICriteria subcriteria =
160+
criteria.GetCriteriaByAlias(_entityAlias)
161+
?? throw new HibernateException($"Criteria\\QueryOver alias '{_entityAlias}' for entity projection is not found.");
162+
163+
var entityName =
164+
criteriaQuery.GetEntityName(subcriteria)
165+
?? throw new HibernateException($"Criteria\\QueryOver alias '{_entityAlias}' is not associated with an entity.");
167166

168-
ICriteria subcriteria = criteria.GetCriteriaByAlias(_entityAlias);
169-
if (subcriteria == null)
170-
throw new HibernateException($"Criteria\\QueryOver alias '{_entityAlias}' for entity projection is not found.");
167+
Persister =
168+
criteriaQuery.Factory.GetEntityPersister(entityName) as IQueryable
169+
?? throw new HibernateException($"Projecting to entities requires a '{typeof(IQueryable).FullName}' persister, '{entityName}' does not have one.");
170+
171+
if (_entityType == null)
172+
{
173+
_entityType = Persister.Type.ReturnedClass;
174+
}
171175

172176
TableAlias = criteriaQuery.GetSQLAlias(
173177
subcriteria,

0 commit comments

Comments
 (0)