这几天,在多队多的中间表的操作上遇到了点麻烦,总是不能删除和更新中间表,因为,我使用的是一队多的关联,
映射的是中间表,所以,我虽然操作了set,但是总是不能反映到中间表上去。
后来使用如下的方法来解决,看如下的例子:
(1)三个表
CREATE TABLE `poi` (
`poi_id` INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`poi_name` VARCHAR(45) COLLATE latin1_swedish_ci NOT NULL DEFAULT '',
PRIMARY KEY (`poi_id`),
UNIQUE KEY `poi_name` (`poi_name`)
)ENGINE=InnoDB
AUTO_INCREMENT=15 CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci'
COMMENT='InnoDB free: 3072 kB';CREATE TABLE `user` (
`user_id` INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(45) COLLATE latin1_swedish_ci NOT NULL DEFAULT '',
`password` VARCHAR(45) COLLATE latin1_swedish_ci NOT NULL DEFAULT '',
PRIMARY KEY (`user_id`),
UNIQUE KEY `username` (`username`)
)ENGINE=InnoDB
AUTO_INCREMENT=16 CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci'
COMMENT='InnoDB free: 3072 kB';
关联表
CREATE TABLE `user_poi` (
`user_id` INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`poi_id` INTEGER(10) UNSIGNED NOT NULL,
KEY `FK_user_pois_poiid` (`poi_id`),
KEY `FK_user_pois_userid` (`user_id`),
CONSTRAINT `FK_user_pois_poiid` FOREIGN KEY (`poi_id`) REFERENCES `poi` (`poi_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_user_pois_userid` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB
AUTO_INCREMENT=3 CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci'
COMMENT='InnoDB free: 3072 kB; (`poi_id`) REFER `demo/poi`(`poi_id`) ON UPDATE CASCADE; (';(2)POJO
/**
* @hibernate.class table="user"
*/
public class User implements java.io.Serializable {
// Fields
private Integer userid;
private String username;
private String password;
/**
* Poi set
*/
private Set<Poi> pois = new HashSet();
// Constructors
/**
* @hibernate.set table="user_poi" cascade="all"
* @hibernate.collection-key column="user_id"
* @hibernate.collection-many-to-many column="poi_id" class="org.zy.pro.pd.dao.Poi"
*/
public Set getPois() {
return pois;
}
public void setPois(Set pois) {
this.pois = pois;
}
public User() {
}
/** full constructor */
public User(String username, String password) {
this.username = username;
this.password = password;
}
// Property accessors
/**
* @hibernate.id column="user_id" generator-class="increment"
*/
public Integer getUserid() {
return this.userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
/**
* @hibernate.property column="username"
* @return
*/
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
/**
* @hibernate.property column="password"
* @return
*/
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
/**
* @hibernate.class table="poi"
*/
public class Poi implements java.io.Serializable {
// Fields
private Integer poiId;
private String poiName;
// Constructors
/** default constructor */
public Poi() {
}
/** minimal constructor */
public Poi(String poiName) {
this.poiName = poiName;
}
/**
* @hibernate.id column="poi_id" generator-class="increment"
*/
public Integer getPoiId() {
return this.poiId;
}
public void setPoiId(Integer poiId) {
this.poiId = poiId;
}
/**
* @hibernate.property column="poi_name"
* @return
*/
public String getPoiName() {
return this.poiName;
}
public void setPoiName(String poiName) {
this.poiName = poiName;
}
}(3)hibernate mapping
利用xDolect根据bean生成映射
Poi.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="org.zy.pro.pd.dao.Poi"
table="poi"
>
<id
name="poiId"
column="poi_id"
type="java.lang.Integer"
>
<generator class="increment">
</generator>
</id>
<property
name="poiName"
type="java.lang.String"
update="true"
insert="true"
column="poi_name"
/>
</class>
</hibernate-mapping>User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="org.zy.pro.pd.dao.User"
table="user"
>
<id
name="userid"
column="user_id"
type="java.lang.Integer"
>
<generator class="increment">
</generator>
</id>
<set
name="pois"
table="user_poi"
lazy="false"
cascade="all"
sort="unsorted"
>
<key
column="user_id"
>
</key>
<many-to-many
class="org.zy.pro.pd.dao.Poi"
column="poi_id"
outer-join="auto"
/>
</set>
<property
name="username"
type="java.lang.String"
update="true"
insert="true"
column="username"
/>
<property
name="password"
type="java.lang.String"
update="true"
insert="true"
column="password"
/>
</class>
</hibernate-mapping>
(4)关联添加
UserDAO ud = new UserDAO();
List l = ud.findAll();
assertNotNull(l);
User u = ud.findById(2);
assertNotNull(u);
Set pois = u.getPois();
Poi p = new Poi("Moc"+new java.util.Random().nextInt(1000000));
System.out.print(p.getPoiId());
pois.add(p);
//关联删除
// u.setPois(null);
Poi pp = pd.findById(12);
pois.remove(pp);
ud.begin();
ud.merge(u);
ud.commit();
// 关联更新也可以,只要改变set就可以了。
使用总结
[1]使用多对多关系
在映射的是时候要使用many-to-many
所以,在user的set集合里面直接存放的是Poi的对象,而不是user_poi表的对象
[2]reverse : false
允许反转操作
[3]casecade : all
只要允许级联操作才可以实现级联的删除和更新
如果使用一对多,就只能更新中间表的普通字段,不能更新主键字段,也不能删除中间表记录
所以只能维护中间表的关联,但是不能更新中间表的信息。
|----------------------------------------------------------------------------------------|
版权声明 版权所有 @zhyiwww
引用请注明来源 http://www.blogjava.net/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2009-02-16 13:14
zhyiwww 阅读(10975)
评论(7) 编辑 收藏 所属分类:
java basic 、
j2ee