One to Many && Many to One mapping.

Discussions

Web tier: servlets, JSP, Web frameworks: One to Many && Many to One mapping.

  1. One to Many && Many to One mapping. (2 messages)

    Hi,

    I am trying "<many-to-one name>" && "<one-to-many>".[Ref : http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=newtopic&f=78&java]

    Team --> Player..

    Data is getting inserted in Teams table, but "team_id" column in the players table is NULL.
    I think team_id's will be automatically inserted by Hibernat{using <id>} and both have to be same to maintain the relation..

    Hence, when i try to fetch the data;

    i.e. First I fetch Teams and from Teams i am trying to get-Players(). Players Set is not having any values because there is no mapping between Team <--> Player..

    Can anyone tell me what am i missing.

    *] Is there anything wrong in xmls.
    *] DB mapping need to be changed. Currently i have team_id from Teams && player_id from Players are PK's.
     
    Team.hbm.xml

    <hibernate-mapping>
    <class name="com.test.Team" table="dbo.teams">
    <id name="id" column="team_id" >
    <generator class="hilo"/>
    </id>
    <property name="name" column="team_name" />
    <property name="city" column="city" />
    <set name="players" cascade="all" inverse="true" lazy="true">
    <key column="team_id"/>
    <one-to-many class="com.test.Player"/>
    </set>
    </class>
    </hibernate-mapping>


    Player.hbm.xml

    <hibernate-mapping>
    <class name="com.test.Player" table="dbo.players">
    <id name="id" column="player_id">
    <generator class="hilo"/>
    </id>
    <property name="firstName" column="first_name" />
    <property name="lastName" column="last_name" />
    <property name="draftDate" column="draft_date" />
    <property name="annualSalary" column="salary" />
    <property name="jerseyNumber" column="jersey_number" />
    <many-to-one name="team" class="com.test.Team" column="team_id"/>
    </class>
    </hibernate-mapping>

    Code:

    Team team = new Team();
    team.setCity("City");
    team.setName("Name");
    // team.setId(001);
    // session.save(team);

    Player player = new Player();
    player.setFirstName("FN");
    player.setLastName("LN");
    player.setAnnualSalary(100);
    // player.setDraftDate(new java.util.Date());
    player.setCity("City");
    player.setJerseyNumber(36471);

    Set players = new HashSet();
    players.add(player);

    team.setPlayers(players);
    session.saveOrUpdate(team);
    tx.commit();
    System.out.println("Saved Team details..");
     

    Any help would be highly appreciated..

    Thanks in Advance..
    /Shridhar..
  2. Team.hbm.xml

    <hibernate-mapping>
    <class name="com.test.Team" table="dbo.teams">
    <id name="id" column="team_id" >
    <generator class="hilo"/>
    </id>
    <property name="name" column="team_name" />
    <property name="city" column="city" />
    <set name="players" cascade="all" inverse="false" lazy="true">
    <key column="team_id" not-null="true" />
    <one-to-many class="com.test.Player" not-found="ignore" /></set>
    </class>
    </hibernate-mapping>

    Player.hbm.xml

    <hibernate-mapping>
    <class name="com.test.Player" table="dbo.players" dynamic-insert="true" dynamic-update="true">
    <id name="id" column="player_id">
    <generator class="hilo"/>
    </id>
    <property name="teamId" column="team_id" type="java.lang.Integer" insert="false" update="false" not-null="true" />
    <property name="firstName" column="first_name" />
    <property name="lastName" column="last_name" />
    <property name="draftDate" column="draft_date" />
    <property name="annualSalary" column="salary" />
    <property name="jerseyNumber" column="jersey_number" />
    <many-to-one name="team" class="com.test.Team" lazy="proxy"
                         not-null="true"
                         column="team_id"
                         fetch="join" />
    </class>
    </hibernate-mapping>
  3. you can check http://www.techfaq360.com/tutorial/many_to_many.jsp This is also good one with code