论坛首页 Java企业应用论坛

Hibernate双向关联插入数据时候的怪问题

浏览 3767 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-09-17  

简单描述一下表结构:主表Team,从表Player,建立外键关联。

以下是HBM文件:

Team:

<hibernate-mapping></hibernate-mapping>

xml 代码
  1. <hibernate-mapping>  
  2.     <class name="com.hibernatedemo.model.Team" table="team" catalog="hibernatedemo">  
  3.         <comment></comment>  
  4.         <id name="id" type="string">  
  5.             <column name="id" length="32" />  
  6.             <generator class="assigned" />  
  7.         </id>  
  8.         <property name="name" type="string">  
  9.             <column name="name" length="32" not-null="true">  
  10.                 <comment></comment>  
  11.             </column>  
  12.         </property>  
  13.         <set name="players" inverse="true" cascade="all">  
  14.             <key>  
  15.                 <column name="team" length="32" not-null="true">  
  16.                     <comment></comment>  
  17.                 </column>  
  18.             </key>  
  19.             <one-to-many class="com.hibernatedemo.model.Player" />  
  20.         </set>  
  21.     </class>  
  22. </hibernate-mapping>  



Palyer:

<hibernate-mapping></hibernate-mapping>

xml 代码
  1. <hibernate-mapping>  
  2.     <class name="com.hibernatedemo.model.Player" table="player" catalog="hibernatedemo">  
  3.         <comment></comment>  
  4.         <id name="id" type="string">  
  5.             <column name="id" length="32" />  
  6.             <generator class="assigned" />  
  7.         </id>  
  8.         <many-to-one name="team" class="com.hibernatedemo.model.Team" fetch="select">  
  9.             <column name="team" length="32" not-null="true">  
  10.                 <comment></comment>  
  11.             </column>  
  12.         </many-to-one>  
  13.         <property name="name" type="string">  
  14.             <column name="name" length="32" not-null="true">  
  15.                 <comment></comment>  
  16.             </column>  
  17.         </property>  
  18.         <property name="gender" type="java.lang.Boolean">  
  19.             <column name="gender">  
  20.                 <comment></comment>  
  21.             </column>  
  22.         </property>  
  23.         <property name="birthday" type="date">  
  24.             <column name="birthday" length="10">  
  25.                 <comment></comment>  
  26.             </column>  
  27.         </property>  
  28.     </class>  
  29. </hibernate-mapping>  

 

插入数据的代码如下:

java 代码
  1. Transaction tx = session.beginTransaction();   
  2.         for(int i=0;i<2;i++) {   
  3.             Team team = new Team();   
  4.             team.setId("Team" + new Integer(i).toString());   
  5.             team.setName(team.getId());   
  6.             Set <player></player>  players = new HashSet <player></player> ();   
  7.             for(int j=0;j<10;j++) {   
  8.                 Player player = new Player();   
  9.                 player.setId("Player" + team.getId()+"."+new Integer(j).toString());   
  10.                 player.setName(player.getId());   
  11.                 player.setBirthday(new Date());   
  12.                 player.setGender(true);   
  13.                 player.setTeam(team);   
  14.                 players.add(player);   
  15.             }   
  16.             team.setPlayers(players);   
  17.             session.save(team);   
  18.         }   
  19.         tx.commit();  

 

执行代码,SQL打印如下:

Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: select player_.id, player_.team as team1_, player_.name as name1_, player_.gender as gender1_, player_.birthday as birthday1_ from hibernatedemo.player player_ where player_.id=?
Hibernate: insert into hibernatedemo.team (name, id) values (?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.team (name, id) values (?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)

 

问题是:为什么在insert语句之前,会执行select语句,对player进行查询呢?请指教

   发表时间:2007-09-29  
没人能回答吗?
0 请登录后投票
   发表时间:2007-10-09  
哈哈,你这个问题的关键就在“cascade="all"”,如果你这样设置了,你对集合做的所有操作都会反映到数据库操作中,这包括你做的“player.setTeam(team);”,导致了那个查询语句。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics