Hibernate双向关联插入数据时候的怪问题
简单描述一下表结构:主表Team,从表Player,建立外键关联。以下是HBM文件:
Team:<hibernate-mapping></hibernate-mapping>
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:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:selectplayer_.id,player_.teamasteam1_,player_.nameasname1_,player_.genderasgender1_,player_.birthdayasbirthday1_fromhibernatedemo.playerplayer_whereplayer_.id=?
Hibernate:insertintohibernatedemo.team(name,id)values(?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.team(name,id)values(?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate:insertintohibernatedemo.player(team,name,gender,birthday,id)values(?,?,?,?,?)
Hibernate: insert into hibernatedemo.player (team, name, gender, birthday, id) values (?, ?, ?, ?, ?)问题是:为什么在insert语句之前,会执行select语句,对player进行查询呢?请指教