简单描述一下表结构:主表Team,从表Player,建立外键关联。
以下是HBM文件:
Team:
<hibernate-mapping></hibernate-mapping>
xml 代码
- <hibernate-mapping>
- <class name="com.hibernatedemo.model.Team" table="team" catalog="hibernatedemo">
- <comment></comment>
- <id name="id" type="string">
- <column name="id" length="32" />
- <generator class="assigned" />
- </id>
- <property name="name" type="string">
- <column name="name" length="32" not-null="true">
- <comment></comment>
- </column>
- </property>
- <set name="players" inverse="true" cascade="all">
- <key>
- <column name="team" length="32" not-null="true">
- <comment></comment>
- </column>
- </key>
- <one-to-many class="com.hibernatedemo.model.Player" />
- </set>
- </class>
- </hibernate-mapping>
Palyer:
<hibernate-mapping></hibernate-mapping>
xml 代码
- <hibernate-mapping>
- <class name="com.hibernatedemo.model.Player" table="player" catalog="hibernatedemo">
- <comment></comment>
- <id name="id" type="string">
- <column name="id" length="32" />
- <generator class="assigned" />
- </id>
- <many-to-one name="team" class="com.hibernatedemo.model.Team" fetch="select">
- <column name="team" length="32" not-null="true">
- <comment></comment>
- </column>
- </many-to-one>
- <property name="name" type="string">
- <column name="name" length="32" not-null="true">
- <comment></comment>
- </column>
- </property>
- <property name="gender" type="java.lang.Boolean">
- <column name="gender">
- <comment></comment>
- </column>
- </property>
- <property name="birthday" type="date">
- <column name="birthday" length="10">
- <comment></comment>
- </column>
- </property>
- </class>
- </hibernate-mapping>
插入数据的代码如下:
java 代码
- Transaction tx = session.beginTransaction();
- for(int i=0;i<2;i++) {
- Team team = new Team();
- team.setId("Team" + new Integer(i).toString());
- team.setName(team.getId());
- Set
<player></player>
players = new HashSet
<player></player>
();
- for(int j=0;j<10;j++) {
- Player player = new Player();
- player.setId("Player" + team.getId()+"."+new Integer(j).toString());
- player.setName(player.getId());
- player.setBirthday(new Date());
- player.setGender(true);
- player.setTeam(team);
- players.add(player);
- }
- team.setPlayers(players);
- session.save(team);
- }
- 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进行查询呢?请指教
分享到:
相关推荐
在实现基于外键的一对多双向关联时,理解注解的含义和作用至关重要,同时还需要考虑性能优化和潜在的问题,确保数据的正确性和一致性。通过阅读源码和实践,我们可以更深入地掌握Hibernate的内在机制。
在Java Web开发中,"Hibernate数据关联实现"是核心概念之一,它涉及到MVC(Model-View-Controller)架构中的Model层,特别是DAO(Data Access Object)模式与Hibernate ORM(对象关系映射)框架的结合使用。...
这篇文章将深入探讨Hibernate中的一对一(One-to-One)唯一外键(Unique Foreign Key)关联映射,尤其是双向关联的实现。这种关联允许在一个实体类中引用另一个实体类,并在另一个实体类中同样设置反向引用,形成...
总之,理解和熟练运用Hibernate的一对多、多对一以及双向关联,是提升Java企业级应用开发效率的关键。通过注解或XML配置,开发者可以灵活地定义实体间的关联,实现数据的持久化。同时,注意级联操作、懒加载策略等...
在这个场景下,每个实体都有一个引用指向另一个实体,形成双向关联。本篇文章将深入探讨如何使用注解和XML配置来实现这种关联。 一、注解方式实现一对一双向外键关联 1. 实体类注解配置 在两个实体类中,我们需要...
### Hibernate关联映射的作用与常用属性详解 #### 关联映射概述 在对象关系映射(Object Relational Mapping,简称ORM)技术中,Hibernate作为Java领域内非常成熟且功能强大的框架之一,它允许开发者将Java类映射...
在Hibernate中,这种关系可以通过`<many-to-many>`标签实现,同时设置双向关联。这意味着在两个实体类中都需要定义相互引用的集合属性。通过双向关联,我们可以从任一侧方便地获取到另一侧的所有关联对象。 其次,...
- **添加关联**:你可以通过`add()`方法向集合中添加对象,Hibernate会自动处理中间表的插入。 - **删除关联**:同样,使用`remove()`方法从集合中移除对象,中间表的相应记录也会被删除。 - **查询关联**:可以...
- **双向关联**:在双向关联中,需要在两个实体类中都进行配置,以确保数据的一致性。 - **外键策略**:外键可以存在于关联的任一方,或者通过中间表来管理,这取决于业务需求和数据库设计。 通过深入学习和实践...
- 解决著名的`n+1`问题,即多次查询相同数据的问题。 - 通过配置适当的缓存策略,可以减少数据库的直接访问次数。 #### 七、`inverse`与二级缓存的关系 在管理双向关联时,`inverse`属性和二级缓存的使用需要仔细...
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. 更复杂...
12. **实体关系映射(E-R Mapping)**:Hibernate支持多种关联关系,如一对一(@OneToOne)、一对多(@OneToMany)、多对一(@ManyToOne)和多对多(@ManyToMany),以及自关联和双向关联。 13. **实体状态**:...
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. 更复杂...
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. 更复杂...
在Java世界中,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. 更复杂...
- 继承关系、多对多关联、双向关联等概念在两种模型间转换时会遇到困难。 - 精粒度模型与粗粒度模型的不同需求也导致了不匹配。 ### 映射继承关系的三种方式 1. **每个具体类对应一张表**:表中包括父类和子类的...
Hibernate支持一对一、一对多、多对一和多对多四种关联关系的映射,包括单向和双向关联,以及懒加载和立即加载策略。 十、性能优化 为了提高性能,可以采用批处理、延迟加载、缓存策略等手段。比如,使用BatchSize...
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. 更复杂的...