c# - Getting Data from all tables instead of one with HQL Query that should only get 1 table's data -


i have created 3 tables in database , put data them. 3 tables have foreign keys joining them together. below table classes , there mappings. when run query listed @ end ilist<> of objects , have data 3 tables. however, hql query top table. how can results top table?

these 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 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 hql code:

iquery q = session.createquery("from technology t"); ilist technologies = q.list(); 

i don't know if possible using hql, using nhibernate's criteria api, can this:

icriteria criteria = session.createcriteria (typeof(technology));  criteria.setfetchmode ("technologydescriptions", fetchmode.lazy);  var list = criteria.list<technology>(); 

however, not want. technologydescriptions won't fetched right now, fetched once access them (that is: first time call technologydescriptions property).

when working nhibernate, shouldn't think in terms of 'data'. rather, should think in terms of 'entities'.
when retrieving entity, want retrieve entity entirly (directly, or in lazy fashion). not possible retrieve entity partially, , quite obvious; should nhibernate entity you've retrieved partially, when try save entity ?

something else pops in mind: suppose want retrieve technologies, , nothing related because want display them in overview or ? in such case, should take @ 'transformations'. instance create additional class called technologyview, looks this:

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 nh     }      public technologyview( int id, string name, string abbreviation )     {        this.id = id;        this.name = name;        this.abbreviation = abbreviation;     } } 

once you've done this, must inform nhibernate existance of class. importing class in hbm.xml file instance . (i not know how using fluent).

<import class="mynamespace.technologyview" /> 

after that, can create query (using hql or criteria) retrieves technologyview instances. nhibernate smart enough generate performant sql query.

using hql:

iquery q = s.createquery ("select new technologyview (t.id, t.name, t.abbreviation) technology t"); 

using criteria:

icriteria criteria = s.createcriteria (typeof(technology)); criteria.setresulttransformer (transformers.aliastobean (typeof(technologyview)); var result = criteria.list<technologyview>(); 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -