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