hibernate多对多为什么会多生成一条delete语句(在线等答案)

sunliming04 2006-07-11 11:02:56
<!-- 多对多的成绩-->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.database">
<class
name="Cource"
table="cource"
>
<meta attribute="sync-DAO">false</meta>
<id
name="Id"
type="string"
column="id"
>
<generator class="uuid.hex"/>
</id>

<property
name="Name"
column="name"
type="string"
not-null="false"
length="50"
/>
<!-- 多对多关联 -->
<set name="stu" table="stu_des" cascade="save-update">
<key column="cor_id"/>
<many-to-many class="com.database.Student" column="stu_id"/>
</set>

</class>
</hibernate-mapping>



<!--学生信息表的hibernate-mapping-->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.database">
<class
name="Student"
table="student"
>
<meta attribute="sync-DAO">false</meta>
<id
name="Id"
type="string"
column="id"
>
<generator class="uuid.hex"/>
</id>

<many-to-one
cascade="all"
name="teamId"
column="team_id"
unique="true"
insert="false"
update="false"
class="com.database.Team"
/>
<property
name="Name"
column="name"
type="string"
not-null="false"
length="50"
/>
<property
name="CardID"
column="cardID"
type="string"
not-null="false"
length="50"
/>
<property
name="Age"
column="age"
type="integer"
not-null="false"
length="10"
/>
<one-to-one
name="des"
class="com.database.Des"
fetch="join"
cascade="all"
/>
<set name="cor" table="stu_des" cascade="save-update" inverse="true">
<key column="stu_id"/>
<many-to-many class="com.database.Cource" column="cor_id"/>
</set>

</class>
</hibernate-mapping>

<!--学生表和成绩表的多对多关联表-->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping package="com.database">
<class
name="StuDes"
table="stu_des"
>
<meta attribute="sync-DAO">false</meta>
<id
name="Id"
type="string"
column="id"
>
<generator class="uuid.hex"/>
</id>

<property
name="StuId"
column="stu_id"
type="string"
not-null="true"
length="50"
/>
<property
name="corId"
column="cor_id"
type="string"
not-null="true"
length="50"
/>


</class>
</hibernate-mapping>

<!--测是程序-->
public void TestManytoMany()
{
try {

session=HibernateSessionFactory.currentSession();
Cource cou=(Cource)session.get(Cource.class, "40287d970c5c9d33010c5c9d3b6a0001");
Student stu=(Student)session.get(Student.class, "40287d970c605998010c6059c14a0002");
// stu.setCor(new HashSet<com.database.Cource>());
// stu.getCor().add(cou);
cou.setStu(new HashSet<com.database.Student>());
cou.getStu().add(stu);
session.save(cou);
tran=session.beginTransaction();
//session.save(cor);
tran.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
logger.error(e);
}
}

结果 数据是插入了 但是为什么他要生成一句delete语句 ?
Hibernate: select cource0_.id as id0_, cource0_.name as name4_0_ from cource cource0_ where cource0_.id=?
Hibernate: select student0_.id as id1_, student0_.team_id as team2_0_1_, student0_.name as name0_1_, student0_.cardID as cardID0_1_, student0_.age as age0_1_, des1_.id as id0_, des1_.des as des3_0_, des1_.stu_id as stu3_3_0_ from student student0_ left outer join des des1_ on student0_.id=des1_.id where student0_.id=?
Hibernate: delete from stu_des where cor_id=?
Hibernate: insert into stu_des (cor_id, stu_id) values (?, ?)

这条删除语句把以前添加进来的关联信息全部删除掉了 然后再把要添加的关联信息添加进来
为什么会出现这用问题?我看书上没有生成这句delete语句 这句语句为什么会生成 高手回答一下
在线等答案
...全文
261 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xnxqs 2006-08-10
  • 打赏
  • 举报
回复
benq998(问题没解决,坚决不结贴.解决了还不结贴,极度鄙视.) ( ) 信誉:98 2006-7-11 14:30:11 得分: 0



加入原来有关联如下,
1------a
2------b
3------c
4------d等等,
现在关联更新了,只有
1------b
3------d
如果根据实际需要修改关联好像很麻烦。
如果把原来的全删除,重新设定所有关联,从操作上看应该会很方便。


程序没有出错.
以上正解.
my1982 2006-07-13
  • 打赏
  • 举报
回复
sunliming04 2006-07-11
  • 打赏
  • 举报
回复
高手教一教我 先去吃饭 然后回来等呀 问题没解决 吃饭都不香
benq998 2006-07-11
  • 打赏
  • 举报
回复
加入原来有关联如下,
1------a
2------b
3------c
4------d等等,
现在关联更新了,只有
1------b
3------d
如果根据实际需要修改关联好像很麻烦。
如果把原来的全删除,重新设定所有关联,从操作上看应该会很方便。
sunliming04 2006-07-11
  • 打赏
  • 举报
回复
有没有人呀 都这么长时间了 快点说一下 继续等

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧