How to order a set in a hibernate hbm.xml file?

killdurst
killdurst used Ask the Experts™
on
Hi, I have an object called Game. Game has a set of Teams, so in my Game.hbm.xml there is the following entry: (Please see code below.)

Team contains another object called Organization. Organization has an attribute called "name".

When I did something like Game.getTeams(), I get all the teams but it is not ordered by name. How do I modify the code below so that the teams returned will be ordered by organization.name?

Thanks.
<set name="teams" inverse="true">
            <key>
                <column name="gameId" />
            </key>
            <one-to-many class="com.competition.game.team.Team" />
        </set>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
From memory Sets do not have the concept of order so you'll need to use a different collection (such as a List)

Author

Commented:
com.competition.game.team.Team refers to a table called Team and this table has a column called "organisationId". When I modified the code to the below, it still worked.

The organisationId refers to a record in the organisation table.

I was just wondering if, instead of just sorting it by organisationId, I can sort it by the name column in the organisation table.
<set name="teams" inverse="true" order-by="organisationId">
    <key>
	<column name="gameId" />
    </key>
    <one-to-many class="com.competition.game.team.Team" />
</set>

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
I stand corrected, appears you can order a set
yes, use order-by="name"

Been using too many tools that automate generating beans and mappings for me :)

Author

Commented:
i can't use "name" because the "name" column exists in the "Organisation" table.

1 game contains many teams.
1 team is tied to 1 organisation.
Java Developer
Top Expert 2010
Commented:
Doubt you can order on a related entity, the other entity may not have even been loaded

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial