Retrieving data from all tables instead of one using an HQL query that should receive only 1 data table

I created 3 tables in my database and put the data in them. All three tables have foreign keys connecting them together. Below are the table classes and there are mappings. When I run the query indicated at the end, I get IList <> from the objects and they have data from all three tables. However, my HQL query is only from the top table. How can I get results only from the top table?

These are my classes:

public class Technology
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual int SortOrder { get; set; }
    public virtual string Abbreviation { get; set; }
    public virtual IList<TechnologyDescription> TechnologyDescriptions { get; private set; }

    public Technology()
    {
        TechnologyDescriptions = new List<TechnologyDescription>();
    }

    public virtual void AddTechnologyDescription(TechnologyDescription technologyDescription)
    {
        technologyDescription.Technology = this;
        TechnologyDescriptions.Add(technologyDescription);
    }
}

public class TechnologyDescription
{
    public virtual int Id { get; private set; }
    public virtual Technology Technology { get; set; }
    public virtual string Description { get; set; }
    public virtual DescriptionType DescriptionType { get; set; }
}

public class DescriptionType
{
    public virtual int Id {get; private set;}
    public virtual string Type { get; set; }
}

These are my mapping objects:

public class TechnologyMap : ClassMap<Technology>
{
    public TechnologyMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.SortOrder);
        Map(x => x.Abbreviation);
        HasMany(x => x.TechnologyDescriptions)
                .Inverse()
                .Cascade.All();
    }
}

public class TechnologyDescriptionMap  : ClassMap<TechnologyDescription>
{
    public TechnologyDescriptionMap()
    {
        Id(x => x.Id);
        References(x => x.Technology);
        Map(x => x.Description);
        References(x => x.DescriptionType);
    }
}

public class DescriptionTypeMap : ClassMap<DescriptionType>
{
    public DescriptionTypeMap()
    {
        Id(x => x.Id);
        Map(x => x.Type);
    }
}

And this is my HQL code:

IQuery q = session.CreateQuery("from Technology T");
IList technologies = q.List();
+3
source share
2 answers

, HQL, API- NHibernate, :

ICriteria criteria = session.CreateCriteria (typeof(Technology));

criteria.SetFetchMode ("TechnologyDescriptions", FetchMode.Lazy);

var list = criteria.List<Technology>();

, , , . , ( : TechnologyDescriptions).

NHibernate "". , "".
Entirly ( ). , ; NHibernate , , ?

- , : , , , - ? "". , , TechnologyView, :

public class TechnologyView
{
    public int Id
    {
        get;
        private set;
    }

    public string Name
    {
        get;
        private set;
    }

    public string Abbreviation
    {
        get;
        private set;
    }

    private TechnologyView()
    {
       // Private constructor, required for NH
    }

    public TechnologyView( int id, string name, string abbreviation )
    {
       this.Id = id;
       this.Name = name;
       this.Abbreviation = abbreviation;
    }
}

, NHibernate . , hbm.xml, . ( , , Fluent).

<import class="MyNamespace.TechnologyView" />

( HQL ), TechnologyView. NHibernate , SQL-.

HQL:

IQuery q = s.CreateQuery ("select new TechnologyView (t.Id, t.Name, t.Abbreviation) from Technology t");

:

ICriteria criteria = s.CreateCriteria (typeof(Technology));
criteria.SetResultTransformer (Transformers.AliasToBean (typeof(TechnologyView));
var result = criteria.List<TechnologyView>();
+1

, , , - , Description . , (NHibernate db. , N + 1 , . )

NHibernate xml . , Fluent NHibernate . .LazyLoad() .

, : Fluent NHibernate HasMany <T> ?

+1

Source: https://habr.com/ru/post/1728709/


All Articles