Hibernate saving children HELP!

Discussions

General J2EE: Hibernate saving children HELP!

  1. Hibernate saving children HELP! (4 messages)

    Hi,

     I am new to Hibernate and having difficulties saving all child objects when saving a parent class (in a many-to-one relationship).

    Player.hbm.xml
    --------------
    <hibernate-mapping>

      <class name="example.Player" table="players">
        <id name="id" column="player_id" type="long" unsaved-value="null">
          <generator class="hilo"/>
        </id>
        <property name="firstName" column="first_name" type="string" length="12" not-null="true"/>
    <property name="lastName" column="last_name" type="string" length="15" not-null="true"/>
    <property name="draftDate" column="draft_date" type="calendar"/>
    <property name="annualSalary" column="salary" type="float"/>
            <property name="jerseyNumber" column="jersey_number" type="integer" length="2" not-null="true"/>
    <many-to-one name="team" class="example.Team" column="team_id"/>
      </class>

    </hibernate-mapping>

    -----------------------------------------------
    Team.hbm.xml

    <hibernate-mapping>

      <class name="example.Team" table="teams">
        <id name="id" column="team_id" type="long" unsaved-value="null">
          <generator class="hilo"/>
        </id>
        <property name="name" column="team_name" type="string" length="15" not-null="true"/>
        <property name="city" column="city" type="string" length="15" not-null="true"/>
        <set name="players" cascade="all" inverse="true" lazy="true">
          <key column="team_id"/>
          <one-to-many class="example.Player"/>
        </set>
      </class>

    </hibernate-mapping>

    ------------------------------------
    In my Test case I do the following:

       public void testSaveTeam() {
            Team team = new Team();
            team.setCity("Detroit");
            team.setName("Pistons");

            Set players = new HashSet();

            Player p1 = new Player();
            p1.setFirstName("Chucky");
            p1.setLastName("Atkins");
            ....

            Player p2 = new Player();
            p2.setFirstName("Chucky2");
            p2.setLastName("Atkins2");
            ....

            team.setPlayers(players);

            try {
                team = dao.saveTeam(team);
                ....
                ....

    -------------------------------
    When executing the test, the "Team" object is saved and only one player is saved with that team. Can anyone suggest what I might be doing wrong? Why aren't both of the Players get saved?

    Thank you,
    ~ Sam K.

    Threaded Messages (4)

  2. Hibernate saving children HELP![ Go to top ]

    In the example you are using bidirectional one-to-many association. In this case, relationships between parent and children are saved, when you save each child (non-inverse end of association). When you saving parent (inverse end), its Set is ignored.

    To solve the problem, you can only save each child object separately. Cascades cannot help. They are not responsible for this.

    Or you can choose not to use bidirectional association. Then all players will be saved when team is saved. But in this case you won't be able to navigate from player to team.
  3. Thanks![ Go to top ]

    Thanks, but how would I implement a non-bidirectional association in this case?

    ~ Sam
  4. Thanks![ Go to top ]

    In this case try to remove many-to-one association from player and "inverse" clause from team's set of players.
  5. Hi Sam,

    Why don't you try to implement LifeCycle and do the saving there ?

    From Hibernate's reference doc :
    "onSave(), onDelete() and onUpdate() may be used to cascade saves and deletions of dependent objects. This is an alternative to declaring cascaded operations in the mapping file"

    Also, less intrusive, even cleaner, using an Interceptor ?

    From the doc, once again :
    "The Interceptor interface provides callbacks from the session to the application allowing the application to inspect and / or manipulate properties of a persistent object before it is saved, updated, deleted or loaded."

    One of these two options should allow you to keep your bidir rel and still save on cascade...

    Have fun,

    Remi - Who said RTFM ??? :-P