Link to home
Start Free TrialLog in
Avatar of killdurst
killdurst

asked on

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

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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

ASKER

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

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 :)
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.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial