-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: why I loose the relation one-to-many with a commit ?
PostPosted: Tue Jul 05, 2005 7:48 am 
Beginner
Beginner

Joined: Tue Jul 05, 2005 4:44 am
Posts: 40
Location: Paris, France
Hi all,

Hibernate version: 3.0
Name and version of the database you are using: MySQL 4.1.12
Connector: mysql-connector-java-3.0.14.jar


I have a relation one-to-many (one: team, many: players).
when I add a new player to a team, I have no error : I can find the player added with team.getPlayers();
But when I force to commit, I loose the relation between the player and the team.

Thanks for any help.

Team.hbm.xml :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.rte_france.etso.proto.model">
<class name="Team" table="TEAM" lazy="true">
   <id name="id" column="TEAM_ID">
      <generator class="native">         
      </generator>
   </id>
   <property name="name" column="TEAM_NAME"></property>
   <bag name="players" cascade="persist,save-update" inverse="true" lazy="true">
      <key column="TEAM_ID"></key>
      <one-to-many class="Player"></one-to-many>
   </bag>      
</class>   
</hibernate-mapping>


Team.java
Code:
public class Team implements Serializable {
    private Long id;
    private String name;
    private Collection players = new ArrayList();

    /**
     *
     * @param p
     */
    public void addPlayer(Player p) {
      if (p == null)
         throw new IllegalArgumentException("Can't add a null Player.");
        this.getPlayers().add(p);
    }
    [...]
}


To add a new Player
Code:
            Player player = new Player();
            player.setName("foo");
            team.addPlayer(player);
            teamDao.makePersistent(team);


TeamDao implementation for Hibernate
Code:
public class TeamDaoHibernate implements TeamDao {
  public void makePersistent(Team team) throws InfrastructureException {
      try {
          HibernateUtil.getSession().saveOrUpdate(team);
          HibernateUtil.commitTransaction();         
      } catch (HibernateException ex) {
          throw new InfrastructureException(ex);
      }
    }



The generated SQL (show_sql=true):
Insert:
Quote:
Hibernate: insert into PERSON (PLAYER_BIRTHDAY, PERSON_NAME, PERSON_HEIGHT, PERSON_WEIGHT, PLAYER_NUMBER, TEAM_ID, PERSON_TYPE) values (?, ?, ?, ?, ?, ?, 'PLAYER');

Select:
Quote:
Hibernate: select team0_.TEAM_ID as TEAM1_, team0_.TEAM_NAME as TEAM2_1_, team0_.TEAM_WON as TEAM3_1_, team0_.TEAM_LOST as TEAM4_1_, team0_.TEAM_PLAYED as TEAM5_1_, team0_.COACH_ID as COACH6_1_ from TEAM team0_
Hibernate: select players0_.TEAM_ID as TEAM8_1_, players0_.PLAYER_ID as PLAYER1_1_, players0_.PLAYER_ID as PLAYER1_0_, players0_.PLAYER_BIRTHDAY as PLAYER3_0_0_, players0_.PERSON_NAME as PERSON4_0_0_, players0_.PERSON_HEIGHT as PERSON5_0_0_, players0_.PERSON_WEIGHT as PERSON6_0_0_, players0_.PLAYER_NUMBER as PLAYER7_0_0_, players0_.TEAM_ID as TEAM8_0_0_ from PERSON players0_ where players0_.TEAM_ID=?


Result in database
Code:
+-----------+-------------+-----------------+-------------+---------------+---------------+---------------+---------+
| PLAYER_ID | PERSON_TYPE | PLAYER_BIRTHDAY | PERSON_NAME | PERSON_HEIGHT | PERSON_WEIGHT | PLAYER_NUMBER | TEAM_ID |
+-----------+-------------+-----------------+-------------+---------------+---------------+---------------+---------+
|         4 | PLAYER      | NULL            | foo         |            98 |            78 |          NULL |    NULL |
+-----------+-------------+-----------------+-------------+---------------+---------------+---------------+---------+

_________________
Fred


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 8:12 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Follow the tutorial in the Hibernate reference documentation, chapter 2.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 9:15 am 
Beginner
Beginner

Joined: Tue Jul 05, 2005 4:44 am
Posts: 40
Location: Paris, France
christian wrote:
Follow the tutorial in the Hibernate reference documentation, chapter 2.
I Christian,
So, I follow the tutorial ;
I modify the method to add a player and the dao ;
I have the same error.

To add a player :
Code:
            Player player = new Player();
            player.setName("foo");
            teamDao.addPlayer(player, 5);
            return mapping.findForward("reload");

TeamDao implementation for Hibernate
Code:
        Session session = HibernateUtil.currentSession();
        Transaction tx = session.beginTransaction();
        Team team = (Team) session.load(Team.class, teamId);
        team.addPlayer(player);
        tx.commit();
        HibernateUtil.closeSession();   

Thanks for any help.

_________________
Fred


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 9:23 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
There are no players or teams in the tutorial. The whole point is to follow it step by step, if you are unable to isolate your issue. Replicating your error doesn't make much sense.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 5:21 am 
Beginner
Beginner

Joined: Tue Jul 05, 2005 4:44 am
Posts: 40
Location: Paris, France
christian wrote:
There are no players or teams in the tutorial. The whole point is to follow it step by step, if you are unable to isolate your issue. Replicating your error doesn't make much sense.
I Christian,
I find my error : I haven't understood the inverse="true" uses!!

New Team.java
Code:
public class Team implements Serializable {
    private Long id;
    private String name;
    private Collection players = new ArrayList();

    /**
     *
     * @param p
     */
    public void addPlayer(Player p) {
      if (p == null)
         throw new IllegalArgumentException("Can't add a null Player.");
        this.getPlayers().add(p);
        p.setTeam(this);
    }
    [...]
}
Thank You for your help.

_________________
Fred


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.