這幾天,在多隊多的中間表的操作上遇到了點麻煩,總是不能刪除和更新中間表,因為,我使用的是一隊多的關聯,
映射的是中間表,所以,我雖然操作了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.tkk7.com/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2009-02-16 13:14
zhyiwww 閱讀(10997)
評論(7) 編輯 收藏 所屬分類:
java basic 、
j2ee