Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 hibernate annotation多对多问题:insert关系前先delete
yqf0215





发贴: 10
积分: 0
于 2007-10-17 09:32 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我用hbiernate annation写的代码,维护多对多关系时,出了个奇怪的问题:

1:Operator.java


@Entity
//@Table(name="operator")
public class Operator implements java.io.Serializable,IfTableModel {

private static final long serialVersionUID = 1L;
public static final String [] column={"编号","姓名","其他信息"};

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "name",unique=true)
private String name;
@Column(name = "msg")
private String msg;

@ManyToOne(targetEntity=base.Role.class)//,cascade={CascadeType.PERSIST,CascadeType.MERGE} )
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinColumn(name="RoleId")
private Role role;

@ManyToMany(targetEntity=base.Department.class)
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinTable(
name="operator_department ",
joinColumns={@JoinColumn(name="OperatorId")},
inverseJoinColumns={@JoinColumn(name="DepartId")}
)
private List<Department> departs=new ArrayList();

}


2Big Smileepartment.java


@Entity
@Table(name = "department")

public class Department implements java.io.Serializable,IfTableModel {

private static final long serialVersionUID = 1L;
public static final String Id = "id";
public static final String Name = "name";
public static final String [] column={"编号","部门名称"};

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@ManyToMany(targetEntity=base.Operator.class,mappedBy="departs")
private List<Operator> operators=new ArrayList();

}


关系说明:operator和department是多对多的关系,其中,operator端负责关系的维护
3:对operator和department操作的代码

Transaction tx2= se.beginTransaction();
BaseUtil u=new BaseUtil();
List<Department> departs=u.getDepartments();
List<Operator> opers=u.getOperators();
opers.get(0).getDeparts().add(departs.get(0));
opers.get(0).getDeparts().add(departs.get(1));
tx2.commit();

现在期望的sql代码是:insert into operator_department (OperatorId, DepartId) values (?, ?)
这里出现一个奇怪的问题是,现在总是先
delete from operator_department where OperatorId=?
然后才把所有原来的关系重新插入一遍
insert into operator_department (OperatorId, DepartId) values (?, ?)
insert into operator_department (OperatorId, DepartId) values (?, ?)
insert into operator_department (OperatorId, DepartId) values (?, ?)
这样严重影响性能,各位高人帮忙看看什么原因啊?




话题树型展开
人气 标题 作者 字数 发贴时间
12738 hibernate annotation多对多问题:insert关系前先delete yqf0215 2638 2007-10-17 09:32
9907 Re:hibernate annotation多对多问题:insert关系前先delete yqf0215 7 2007-10-24 09:11
9871 Re:hibernate annotation多对多问题:insert关系前先delete JiafanZhou 1954 2007-10-24 17:02
10052 Re:hibernate annotation多对多问题:insert关系前先delete yqf0215 91 2007-10-29 10:18
10228 Re:hibernate annotation多对多问题:insert关系前先delete JiafanZhou 451 2007-11-30 23:23

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923