Working around Duplicate association path bug in Nhibernate with Query Over -


i've got bit of code tries access same association path twice , they're same aliases because i'm using query objects, have them in 2 different places , not sure how alias.

may code can clear confusion:

var privateblogquery = new blogquery()     .byuser(1)     .removehidden()     .fetchuserdetails();   //<-------- in blog query object class: ------>  /// gets private blogs user has permissions view public blogquery byuser(int userid) {     var authorisedusers = null;      this.left.joinqueryover(r => r.authorisedusers, () => authorisedusers)         .where(r => r.user.id == userid);      return this; }  /// removes private blogs user has marked hidden public blogquery removehidden() {     var authorisedusers = null;      this.left.joinqueryover(r => r.authorisedusers, () => authorisedusers)         .where(r => !r.ishidden);      return this; }  /// loads details of users have permission  /// view private blog public blogquery fetchuserdetails() {     var users = null;     var authorisedusers = null;      this.left.joinqueryover(r => r.authorisedusers, () => authorisedusers)         .left.joinqueryover(r => r.user, () => users);      return this; } 

there times when i'm using 3 criteria individually , sql generated precisely need , nice , dandy long used separately.

now need use them together, @ same time , nhibernate throws exception duplicate alias , changed alias on these 3 functions greeted duplicate association path exeception.

a bit of googling , learnt bug in hibernate , found few workarounds on bug

trouble using query objects , hence query on , not sure how association path / alias here.

so how go please?

  • make authorisedusers membervariable of blogquery , use marker/flag know if byuser , removehidden should join
  • use joinalias

example

authoriseduser authoriseduser; bool authorisedusersjoined;  public blogquery removehidden() {     if (!authorisedusersjoined)         this.left.joinalias(r => r.authorisedusers, () => authoriseduser);      this.where(() => !authoriseduser.ishidden);      return this; } 

fetchuserdetails mutual exclusive other 2 because filtering on association prevents nh initializing association. you'll need subquery filter , query resulting ids , initialize then.

/// loads details of users have permission  /// view private blog public blogquery fetchuserdetails() {      this.query = queryover.of<blog>()         .whererestrictionon(b => b.id).isin(this.query.select(b => b.id))         .fetch(r => r.authorisedusers).eager             .thenfetch(au => au.user).eager;      return this; } 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -