[疑难杂症]hibernate annotation多对多问题:insert关系前先delete

使用Hibernate注解实现Operator与Department多对多关系时遇到,在更新关系时总是先删除再重新插入的问题。期望直接插入新关系而非删除后再插入。
我用hbiernate annation写的代码,维护多对多关系时,出了个奇怪的问题:

1:Operator.java
[code]

@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)

@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();


}
[/code]

2:Department.java
[code]

@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();

}
[/code]

关系说明:operator和department是多对多的关系,其中,operator端负责关系的维护
3:对operator和department操作的代码
[code]
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();
[/code]
现在期望的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 (?, ?)
这样严重影响性能,各位高人帮忙看看什么原因啊?

奇怪的问题啊!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值