Results 1 to 3 of 3

Thread: Hibernate. Get object by associated one

  1. #1
    Join Date
    May 2009
    Location
    Rus
    Posts
    87

    Default Hibernate. Get object by associated one

    I have class Game that has collection of players. This snippet is from Game.hbm.xml:
    Code:
            <set name="players" access="property" table="players" cascade="all">
                <key column="game_id"/>
                <one-to-many class="Player"/>
            </set>
    How can I get Game if I have one of its players? I use HibernateTemplate, maybe it has any handy methods for this purpose?

  2. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by ctapobep View Post
    I have class Game that has collection of players. This snippet is from Game.hbm.xml:
    Code:
            <set name="players" access="property" table="players" cascade="all">
                <key column="game_id"/>
                <one-to-many class="Player"/>
            </set>
    How can I get Game if I have one of its players? I use HibernateTemplate, maybe it has any handy methods for this purpose?
    You're talking about bidirectional association here. I.e. if there is no 'Player -> Game' association neither spring nor hibernate can help you there.

  3. #3
    Join Date
    May 2009
    Location
    Rus
    Posts
    87

    Default

    Congratulate me! I made it!
    Here it is:
    Code:
            criteria.add(Restrictions.sqlRestriction(
                    "{alias}.id=(select game_id from players where players.id=" + player.getId() +")"));

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •