HasManyToMany OrderBy bug

Original Post 's Avatar

charles.ouellet

22 Mar, 2010 06:46 PM via web

Hi,

We noticed a bug while trying to set an OrderBy on a HasManyToMany collection.

We exported the FluentMappings as Hbm files and we noticed this:

<bag cascade="save-update" inverse="true" name="AuthorizedProjects" table="ProjectAuthorizedUsers" order-by="SaveDate DESC">
  <key>
    <column name="UserId" />
  </key>
  <many-to-many class="Entities.Project, Core, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
    <column name="ProjectId" />
  </many-to-many>
</bag>

We then have an exception that SaveDate is not an existing column.

So we manually changed the hbm file and moved the order-by attribute in the many-to-many node

<bag cascade="save-update" inverse="true" name="AuthorizedProjects" table="ProjectAuthorizedUsers">
  <key>
    <column name="UserId" />
  </key>
  <many-to-many  order-by="SaveDate DESC" class="Entities.Project, Core, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
    <column name="ProjectId" />
  </many-to-many>
</bag>

It worked! So I think this is a bug. Is there a way we can specify where we want to order-by attribute to be?

Thanks a lot,

Charles

  1. 2 Posted by charles.ouellet on 22 Mar, 2010 06:48 PM

    charles.ouellet's Avatar

    I don't know why but in my examples, the first line is missing for both xml blocks.

    Here is the first line for the first block:

    <bag cascade="save-update" inverse="true" name="AuthorizedProjects" table="ProjectAuthorizedUsers" order-by="SaveDate DESC">
    

    Second block:

    <bag cascade="save-update" inverse="true" name="AuthorizedProjects" table="ProjectAuthorizedUsers">
    
  2. 3 Posted by David T on 22 Mar, 2010 06:59 PM

    David T's Avatar

    Hi, I work with Charles,

    Basically in the first case the order-by attribute is on the bag tag, and in the second it's on the many-to-many.

    If I understand it correctly, when on the bag, the order by will work on the columns of the association table, and when on the many-to-many, it will work on the referenced table's columns.

    The behavior we want is the second one, however, we can't figure out how to do it with fluent.

    It seems we will be forced to use a criteria to achieve this, but I think it should be possible to do it in fluent since hbm mappings allow it.

  3. Support Staff 4 Posted by James Gregory on 23 Mar, 2010 02:18 PM

    James Gregory's Avatar

    Hey guys,

    This seems like a bug on our part, or at least a missing method of some kind. The where clause is valid on both elements, but I'm guessing we've incorrectly assumed they're interchangeable.

    It should just be a case of adding something like the following to the ManyToManyPart:

    public ManyToManyPart<TChild> ManyToManyWhere(string where)
    {
      relationshipAttributes.Set(x => x.Where, where);
      return this;
    }
    

    I'll need to put some tests in there as well, but if you need it urgently then you should be able to stick that into your own copy of the code for the time being.

  4. 5 Posted by David T on 23 Mar, 2010 06:01 PM

    David T's Avatar

    Thanks for the suggestion!

    We implemented it using a query, but if fluent nhibernate gets updated to support this we will be sure to use it.

  5. 6 Posted by Dmitri V on 21 Apr, 2010 07:44 AM

    Dmitri V's Avatar

    When this bug will be fixed?
    We use xml now (((

  6. Support Staff 7 Posted by Paul Batum on 21 Apr, 2010 10:36 AM

    Paul Batum's Avatar

    Okay I've implemented support for this. I've added an additional fluent interface method called "OrderByOnRelationshipElement". Pretty ugly name, so I'd love to hear some other suggestions.

    The change is available on my dev branch:
    http://github.com/paulbatum/fluent-nhibernate/tree/dev

    It would be great if someone could let me know whether this resolves the issue.

  7. 8 Posted by Dmitri V on 21 Apr, 2010 12:56 PM

    Dmitri V's Avatar

    Paul Batum.
    Thank you so much, I have viewed your solution, downloaded 636 build sources and added 2 methods to ManyToManyPart:
    ChildWhere - adds "where" to many-to-many element
    ChildOrderBy - adds "order-by" to many-to-many element
    Thank you very much!

  8. Support Staff 9 Posted by Paul Batum on 25 Apr, 2010 01:49 PM

    Paul Batum's Avatar

    I like the sound of ChildWhere and ChildOrderBy. I think I'll make the same changes that you did Dmitri and see that they work their way into the trunk.

  9. Support Staff 10 Posted by Paul Batum on 02 May, 2010 03:08 AM

    Paul Batum's Avatar

    I added your suggested methods Dmitri. They'll be in the official trunk in a few days.

  10. Paul Batum resolved this discussion on 02 May, 2010 03:08 AM.

Comments are currently closed for this discussion. You can start a new one.

Recent Discussions

05 Jul, 2010 10:29 PM
05 Jul, 2010 12:45 PM
05 Jul, 2010 12:42 PM
05 Jul, 2010 12:17 PM
05 Jul, 2010 12:12 PM

 

03 Jul, 2010 12:26 AM
02 Jul, 2010 02:17 PM
02 Jul, 2010 08:18 AM
02 Jul, 2010 12:20 AM
01 Jul, 2010 10:14 PM