Hibernate: create references??

From: david (debitsudo_at_gmx.net)
Date: 05/12/04


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);
// }
        
    }



Relevant Pages

  • Hibernate die richtige Wahl =?ISO-8859-15?Q?f=FCr_mich=3F?=
    ... Ich möchte mich mal mehr in die Programmierung "richtiger" Java ... Anwendung geteilte Session factory? ... Wenn ich versuche Hibernate nur in möglichst niedrigen Schichten zu ...
    (de.comp.lang.java)
  • Re: [Hibernate] wechselnde Fehler bei Update
    ... Denn ich kann mir nicht vorstellen, dass man Objekte nicht ordentlich upgedatet bekommt nur weil lazy loading aktiv ist. ... Der TilesController holt sich bei der Factory die Session. ... Ich würde Dir ein Buch wie Hibernate in Action empfehlen, das diesen Herbst auch in einer Neuauflage rauskommt und dann Hibernate 3 behandelt. ...
    (de.comp.lang.java)
  • Hibernate all processes in a KDE desktop?
    ... be able to suspend a single process and then shut down my ... It would be handy to be able to hibernate all processes running as my ... I use KDE 3.5.5. ... On Login and selected "Restore manually saved session". ...
    (alt.os.linux)
  • Re: J2EE (websphere) + Hibernate + ThreadLocal
    ... According to Hibernate team the standard way to use hibernate in a J2EE ... environement (on a session per request basis) is with the HibernateUtil ... Therefore you must leave the thread in a good state for the next request. ...
    (comp.lang.java.programmer)
  • Re: J2EE (websphere) + Hibernate + ThreadLocal
    ... According to Hibernate team the standard way to use hibernate in a J2EE ... environement (on a session per request basis) is with the HibernateUtil ...
    (comp.lang.java.programmer)