Hibernate saving children HELP!


General J2EE: Hibernate saving children HELP!

  1. Hibernate saving children HELP! (4 messages)


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


      <class name="example.Player" table="players">
        <id name="id" column="player_id" type="long" unsaved-value="null">
          <generator class="hilo"/>
        <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 name="example.Team" table="teams">
        <id name="id" column="team_id" type="long" unsaved-value="null">
          <generator class="hilo"/>
        <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"/>


    In my Test case I do the following:

       public void testSaveTeam() {
            Team team = new Team();

            Set players = new HashSet();

            Player p1 = new Player();

            Player p2 = new Player();


            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