-->
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.  [ 3 posts ] 
Author Message
 Post subject: References in Hibernate
PostPosted: Tue May 18, 2004 2:43 am 
Newbie

Joined: Thu May 13, 2004 11:07 am
Posts: 8
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 18, 2004 2:44 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This is the correct behavior (although your implementation of the accessor methods is not perfect): You want a bidirectional relationship, you set both "ends" in Java.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 18, 2004 2:45 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is expected,
Check the parent/child relationship champter of the ref guide and this page too http://www.hibernate.org/155.html

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.