Hibernate: create references??
From: david (debitsudo_at_gmx.net)
Date: 05/12/04
- Next message: Axel Hallez: "Re: "No suitable driver" is a classpath problem?"
- Previous message: Netto: ""No suitable driver" is a classpath problem?"
- Next in thread: Adam Maass: "Re: Hibernate: create references??"
- Reply: Adam Maass: "Re: Hibernate: create references??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 12 May 2004 05:23:32 -0700
we have set up a simple test environment for a hibernate. We've
followed the example of
http://www.systemmobile.com/articles/IntroductionToHibernate.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 was 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:
<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:
<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:
<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
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);
// }
}
- Next message: Axel Hallez: "Re: "No suitable driver" is a classpath problem?"
- Previous message: Netto: ""No suitable driver" is a classpath problem?"
- Next in thread: Adam Maass: "Re: Hibernate: create references??"
- Reply: Adam Maass: "Re: Hibernate: create references??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|