we have set up a simple test environment for a hibernate. We've
followed the example of
http://www.systemmobile.com/articles/In ... rnate.html by
Nick Heudecker. (using hbm2java for java code-generation).
We have two classes (Team, Player) which map to respective tables.
(see code below).
When executing
team.setPlayers(players);
Session session = sessionFactory.openSession();
session.saveOrUpdate(team);
I'd expect to have the team_id inserted for each player.
However, this does only happen if the code is inserted into the
Team.setPlayers-method manually (commented out in the example).
Is this the way it should be? Or better: Is there a way that the
references within the tables are updated automatically?
Thanks for any hint
david
Here are code excerpts (I'm happy to post more on request):
====
hibernate.cfg.xml:
Code:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.username">USER1</property>
<property name="hibernate.connection.password">GUESSME</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@//networkaddress:1521/testdb</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="team.hbm.xml" />
<mapping resource="player.hbm.xml" />
</session-factory>
</hibernate-configuration>
====
player.hbm.xml:
Code:
<hibernate-mapping>
<class name="Player" table="players">
<id name="id" column="player_id" type="long" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="lastName" column="last_name" type="string"
length="15" not-null="true"/>
<many-to-one name="team" class="Team" column="team_id"/>
</class>
</hibernate-mapping>
====
team.hbm.xml:
Code:
<hibernate-mapping>
<class name="Team" table="teams">
<id name="id" column="team_id" type="long" unsaved-value="null">
<generator class="increment"/>
</id>
<property name="name" column="team_name" type="string"
length="15" not-null="true"/>
<set name="players" cascade="all" inverse="true" lazy="true">
<key column="team_id"/>
<one-to-many class="Player"/>
</set>
</class>
</hibernate-mapping>
====
Team.java
Code:
public void setPlayers(Set players) {
this.players = players;
// the following code does what I want Hibernate to do:
// for(Iterator it=players.iterator(); it.hasNext();){
// Player player = (Player) it.next();
// player.setTeam(this);
// }
}
====
Main:
Code:
Configuration cfg = new Configuration();
SessionFactory sessionFactory = cfg.configure().buildSessionFactory();
Team team = new Team();
team.setName("FCZ");
System.out.println("3");
Player player = new Player();
player.setFirstName("Daniel");
player.setLastName("Gygax");
player.setJerseyNumber(7);
player.setAnnualSalary(40009f);
Set players = new HashSet();
players.add(player);
team.setPlayers(players);
Session session = sessionFactory.openSession();
session.saveOrUpdate(team);
session.flush();
session.connection().commit();
session.close();
Database: Oracle 9i
Hibernate: 2.1.3
Note: sorry for cross posting. this topic has been posted before on comp.lang.java.databases . I hope to get more response here.