Hibernate中对象一对多关系的save问题?

03-09-11 新疆朋友

现有两个对象Team , Player为一对多关系映射文件如下:
team.hbm.xml :
<hibernate-mapping>
<class name="example.Team" table="teams">
<id name="id" column="team_id" type="long" unsaved-value="null">
<generator class="hilo"/>
</id>
<property name="name" column="team_name" type="string" length="15" not-null="true"/>
<property name="city" column="city" type="string" length="15" not-null="true"/>
<set name="players" cascade="all" inverse="true" lazy="true">
<key column="team_id"/>
<one-to-many class="example.Player"/>
</set>
</class>
</hibernate-mapping>
player.hbm.xml :
<hibernate-mapping>
<class name="example.Player" table="players">
<id name="id" column="player_id" type="long" unsaved-value="null">
<generator class="hilo"/>
</id>
<property name="firstName" column="first_name" type="string" length="12" not-null="true"/>
<property name="lastName" column="last_name" type="string" length="15" not-null="true"/>
<property name="draftDate" column="draft_date" type="date"/>
<property name="annualSalary" column="salary" type="float"/>
<property name="jerseyNumber" column="jersey_number" type="integer" length="2" not-null="true"/>
<many-to-one name="team" class="example.Team" column="team_id"/>
</class>
</hibernate-mapping>
使用下列程序测试:
Player p1 = new Player() ;
Player p2 = new Player() ;
Team m1 = new Team() ;
Set players = new HashSet();
try {
m1.setCity("karamay");
m1.setId(new Long(990));
m1.setName("market");


p1.setAnnualSalary(40000);
p1.setDraftDate(new Date(System.currentTimeMillis()));
p1.setFirstName("Jason");
p1.setJerseyNumber(93);
p1.setLastName("mmt");
p1.setTeam(m1);
players.add(p1);

p2.setAnnualSalary(60000);
p2.setDraftDate(new Date(System.currentTimeMillis()));
p2.setFirstName("Michel");
p2.setJerseyNumber(23);
p2.setLastName("rlh");
p2.setTeam(m1);
players.add(p2);

m1.setPlayers(players);

session.save(m1);
session.flush();
session.connection().commit();
} catch (HibernateException he) {
logger.error("savePlayer hibernate error");
he.printStackTrace();
} catch (SQLException se) {
logger.error("savePlayer sql error");
se.printStackTrace();
}
运行结果发现,数据库中TEAM表正确,新增了一条记录,但是PLAYER表只增加了一条记录,是P1对象的,查看了LOG4J的调试信息,发现只有两条IINSERT语句,我使用的是ORACLE9I数据库。
请教:是否对于这种一对多的数据存储,不能采用上述方法,只对父对象存储一次,还是对涉及到的每一个对象都要存一次。谢谢高手指点。

新疆朋友
2003-09-11 10:52

player.hbm.xml
《class name="example.Player" table="players"》
《id name="id" column="player_id" type="long" unsaved-value="null"》
《generator class="hilo"/》
《/id》
《property name="firstName" column="first_name" type="string" length="12" not-null="true"/》
《property name="lastName" column="last_name" type="string" length="15" not-null="true"/》
《property name="draftDate" column="draft_date" type="date"/》
《property name="annualSalary" column="salary" type="float"/》
《property name="jerseyNumber" column="jersey_number" type="integer" length="2" not-null="true"/》
《many-to-one name="team" class="example.Team" column="team_id"/》
《/class》

《/hibernate-mapping》
team.hbm.xml
《hibernate-mapping》

《class name="example.Team" table="teams"》
《id name="id" column="team_id" type="long" unsaved-value="null"》
《generator class="hilo"/》
《/id》
《property name="name" column="team_name" type="string" length="15" not-null="true"/》
《property name="city" column="city" type="string" length="15" not-null="true"/》
《set name="players" cascade="all" inverse="true" lazy="true"》
《key column="team_id"/》
《one-to-many class="example.Player"/》
《/set》
《/class》

《/hibernate-mapping》

you.cai
2003-09-11 10:53

你的这个问题,hibernate的文档有一章专门讲父子关系的,你看一下。黑很有收获的。

you.cai
2003-09-11 10:56

简单的话,在m里面增加一个方法
public void addPlayer(Player p){
p.setTeam(this);
players.add(p);
}

测试代码:
m1.addPlayer(p1);
m1.addPlayer(p2);