Announcement Announcement Module
Collapse
No announcement yet.
Hibernate. Get object by associated one Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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() +")"));

      Comment

      Working...
      X