Skip to content

Fixed entity name retrieval for EntityProjection #1534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/NHibernate.Test/Async/Criteria/EntityProjectionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ namespace NHibernate.Test.Criteria
[TestFixture]
public class EntityProjectionsTestAsync : TestCaseMappingByCode
{
private const string customEntityName = "CustomEntityName";
private EntityWithCompositeId _entityWithCompositeId;
private EntityCustomEntityName _entityWithCustomEntityName;

protected override HbmMapping GetMappings()
{
Expand Down Expand Up @@ -76,6 +78,16 @@ protected override HbmMapping GetMappings()

rc.Property(e => e.Name);
});

mapper.Class<EntityCustomEntityName>(
rc =>
{
rc.EntityName(customEntityName);

rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

Expand Down Expand Up @@ -117,6 +129,11 @@ protected override void OnSetUp()
}
};

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

_entityWithCompositeId = new EntityWithCompositeId
{
Key = new CompositeKey
Expand All @@ -132,6 +149,7 @@ protected override void OnSetUp()
session.Save(parent.SameTypeChild);
session.Save(parent);
session.Save(_entityWithCompositeId);
session.Save(customEntityName, _entityWithCustomEntityName);

session.Flush();
transaction.Commit();
Expand Down Expand Up @@ -459,5 +477,22 @@ public async Task EntityProjectionForCompositeKeyLazyAsync()
Assert.That(NHibernateUtil.IsInitialized(composite), Is.False, "Object must be lazy loaded.");
}
}

[Test]
public async Task EntityProjectionForCustomEntityNameAsync()
{
using (var session = OpenSession())
{
var entity = await (session
.QueryOver<EntityCustomEntityName>(customEntityName)
.Select(Projections.RootEntity())
.Take(1).SingleOrDefaultAsync());

Assert.That(entity, Is.Not.Null);
Assert.That(NHibernateUtil.IsInitialized(entity), Is.True, "Object must be initialized.");
Assert.That(entity.Id, Is.EqualTo(_entityWithCustomEntityName.Id));
Assert.That(entity.Name, Is.EqualTo(_entityWithCustomEntityName.Name));
}
}
}
}
6 changes: 6 additions & 0 deletions src/NHibernate.Test/Criteria/EntityProjectionsEntities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ public class EntitySimpleChild
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}

public class EntityCustomEntityName
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}

public class EntityComplex
{
Expand Down
35 changes: 35 additions & 0 deletions src/NHibernate.Test/Criteria/EntityProjectionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ namespace NHibernate.Test.Criteria
[TestFixture]
public class EntityProjectionsTest : TestCaseMappingByCode
{
private const string customEntityName = "CustomEntityName";
private EntityWithCompositeId _entityWithCompositeId;
private EntityCustomEntityName _entityWithCustomEntityName;

protected override HbmMapping GetMappings()
{
Expand Down Expand Up @@ -65,6 +67,16 @@ protected override HbmMapping GetMappings()

rc.Property(e => e.Name);
});

mapper.Class<EntityCustomEntityName>(
rc =>
{
rc.EntityName(customEntityName);

rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

Expand Down Expand Up @@ -106,6 +118,11 @@ protected override void OnSetUp()
}
};

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

_entityWithCompositeId = new EntityWithCompositeId
{
Key = new CompositeKey
Expand All @@ -121,6 +138,7 @@ protected override void OnSetUp()
session.Save(parent.SameTypeChild);
session.Save(parent);
session.Save(_entityWithCompositeId);
session.Save(customEntityName, _entityWithCustomEntityName);

session.Flush();
transaction.Commit();
Expand Down Expand Up @@ -448,5 +466,22 @@ public void EntityProjectionForCompositeKeyLazy()
Assert.That(NHibernateUtil.IsInitialized(composite), Is.False, "Object must be lazy loaded.");
}
}

[Test]
public void EntityProjectionForCustomEntityName()
{
using (var session = OpenSession())
{
var entity = session
.QueryOver<EntityCustomEntityName>(customEntityName)
.Select(Projections.RootEntity())
.Take(1).SingleOrDefault();

Assert.That(entity, Is.Not.Null);
Assert.That(NHibernateUtil.IsInitialized(entity), Is.True, "Object must be initialized.");
Assert.That(entity.Id, Is.EqualTo(_entityWithCustomEntityName.Id));
Assert.That(entity.Name, Is.EqualTo(_entityWithCustomEntityName.Name));
}
}
}
}
26 changes: 15 additions & 11 deletions src/NHibernate/Criterion/EntityProjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,27 @@ private void SetFields(ICriteriaQuery criteriaQuery)
entityProjectionQuery.RegisterEntityProjection(this);
}

if (_entityType == null)
{
_entityType = criteria.GetRootEntityTypeIfAvailable();
}

if (_entityAlias == null)
{
_entityAlias = criteria.Alias;
}

Persister = criteriaQuery.Factory.GetEntityPersister(_entityType.FullName) as IQueryable;
if (Persister == null)
throw new HibernateException($"Projecting to entities requires a '{typeof(IQueryable).FullName}' persister, '{_entityType.FullName}' does not have one.");
ICriteria subcriteria =
criteria.GetCriteriaByAlias(_entityAlias)
?? throw new HibernateException($"Criteria\\QueryOver alias '{_entityAlias}' for entity projection is not found.");

var entityName =
criteriaQuery.GetEntityName(subcriteria)
?? throw new HibernateException($"Criteria\\QueryOver alias '{_entityAlias}' is not associated with an entity.");

ICriteria subcriteria = criteria.GetCriteriaByAlias(_entityAlias);
if (subcriteria == null)
throw new HibernateException($"Criteria\\QueryOver alias '{_entityAlias}' for entity projection is not found.");
Persister =
criteriaQuery.Factory.GetEntityPersister(entityName) as IQueryable
?? throw new HibernateException($"Projecting to entities requires a '{typeof(IQueryable).FullName}' persister, '{entityName}' does not have one.");

if (_entityType == null)
{
_entityType = Persister.Type.ReturnedClass;
}

TableAlias = criteriaQuery.GetSQLAlias(
subcriteria,
Expand Down