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 databaseCode:
+-----------+-------------+-----------------+-------------+---------------+---------------+---------------+---------+
| PLAYER_ID | PERSON_TYPE | PLAYER_BIRTHDAY | PERSON_NAME | PERSON_HEIGHT | PERSON_WEIGHT | PLAYER_NUMBER | TEAM_ID |
+-----------+-------------+-----------------+-------------+---------------+---------------+---------------+---------+
| 4 | PLAYER | NULL | foo | 98 | 78 | NULL | NULL |
+-----------+-------------+-----------------+-------------+---------------+---------------+---------------+---------+