`
johnnylzb
  • 浏览: 12793 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类

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

阅读更多

简单描述一下表结构:主表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进行查询呢?请指教

分享到:
评论
2 楼 fangang 2007-10-09  
哈哈,你这个问题的关键就在“cascade="all"”,如果你这样设置了,你对集合做的所有操作都会反映到数据库操作中,这包括你做的“player.setTeam(team);”,导致了那个查询语句。
1 楼 johnnylzb 2007-09-29  
没人能回答吗?

相关推荐

    Hibernate Annotation 基于外键的一对多双向关联

    在实现基于外键的一对多双向关联时,理解注解的含义和作用至关重要,同时还需要考虑性能优化和潜在的问题,确保数据的正确性和一致性。通过阅读源码和实践,我们可以更深入地掌握Hibernate的内在机制。

    Hibernate数据关联实现〖MVSN〗_Struts + DAO + Hibernate

    在Java Web开发中,"Hibernate数据关联实现"是核心概念之一,它涉及到MVC(Model-View-Controller)架构中的Model层,特别是DAO(Data Access Object)模式与Hibernate ORM(对象关系映射)框架的结合使用。...

    Hibernate一对一唯一外键关联映射(双向关联)

    这篇文章将深入探讨Hibernate中的一对一(One-to-One)唯一外键(Unique Foreign Key)关联映射,尤其是双向关联的实现。这种关联允许在一个实体类中引用另一个实体类,并在另一个实体类中同样设置反向引用,形成...

    Hibernate一对多(多对一)双向关联(annotation/xml)

    总之,理解和熟练运用Hibernate的一对多、多对一以及双向关联,是提升Java企业级应用开发效率的关键。通过注解或XML配置,开发者可以灵活地定义实体间的关联,实现数据的持久化。同时,注意级联操作、懒加载策略等...

    Hibernate一对一双向外键关联(annotation/xml)

    在这个场景下,每个实体都有一个引用指向另一个实体,形成双向关联。本篇文章将深入探讨如何使用注解和XML配置来实现这种关联。 一、注解方式实现一对一双向外键关联 1. 实体类注解配置 在两个实体类中,我们需要...

    hibernate关联映射的作用和常用属性解释

    ### Hibernate关联映射的作用与常用属性详解 #### 关联映射概述 在对象关系映射(Object Relational Mapping,简称ORM)技术中,Hibernate作为Java领域内非常成熟且功能强大的框架之一,它允许开发者将Java类映射...

    hibernate 双向 多对对,一对多,一对一 映射的 集合

    在Hibernate中,这种关系可以通过`&lt;many-to-many&gt;`标签实现,同时设置双向关联。这意味着在两个实体类中都需要定义相互引用的集合属性。通过双向关联,我们可以从任一侧方便地获取到另一侧的所有关联对象。 其次,...

    Hibernate多对多关联关系demo

    - **添加关联**:你可以通过`add()`方法向集合中添加对象,Hibernate会自动处理中间表的插入。 - **删除关联**:同样,使用`remove()`方法从集合中移除对象,中间表的相应记录也会被删除。 - **查询关联**:可以...

    HiBERNATE关联映射

    - **双向关联**:在双向关联中,需要在两个实体类中都进行配置,以确保数据的一致性。 - **外键策略**:外键可以存在于关联的任一方,或者通过中间表来管理,这取决于业务需求和数据库设计。 通过深入学习和实践...

    Hibernate性能调优

    - 解决著名的`n+1`问题,即多次查询相同数据的问题。 - 通过配置适当的缓存策略,可以减少数据库的直接访问次数。 #### 七、`inverse`与二级缓存的关系 在管理双向关联时,`inverse`属性和二级缓存的使用需要仔细...

    Hibernate+中文文档

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    Hibernate.zip

    12. **实体关系映射(E-R Mapping)**:Hibernate支持多种关联关系,如一对一(@OneToOne)、一对多(@OneToMany)、多对一(@ManyToOne)和多对多(@ManyToMany),以及自关联和双向关联。 13. **实体状态**:...

    hibernate3.2中文文档(chm格式)

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    HibernateAPI中文版.chm

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    hibernate5--3.映射关系

    在Java世界中,Hibernate是一个非常流行...在使用这些关系时,需要注意双向关联的处理,以及在配置文件中正确设置懒加载和级联操作,以优化性能。同时,合理设计数据库表结构和对象模型,可以避免出现不必要的复杂性。

    Hibernate中文详细学习文档

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    Hibernate常见面试题

    - 继承关系、多对多关联、双向关联等概念在两种模型间转换时会遇到困难。 - 精粒度模型与粗粒度模型的不同需求也导致了不匹配。 ### 映射继承关系的三种方式 1. **每个具体类对应一张表**:表中包括父类和子类的...

    韩顺平老师Hibernate教程PPT(完整)

    Hibernate支持一对一、一对多、多对一和多对多四种关联关系的映射,包括单向和双向关联,以及懒加载和立即加载策略。 十、性能优化 为了提高性能,可以采用批处理、延迟加载、缓存策略等手段。比如,使用BatchSize...

    Hibernate 中文 html 帮助文档

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的...

Global site tag (gtag.js) - Google Analytics