存在的數據我有三個表即personalinfo,groups_designation,groups_desig_category如何處理許多一對多的關係,在Hibernate
- personalinfo:PID(個人ID)
- groups_designation:GID(組ID )
- groups_desig_category:GID,PID
其實我有兩個表的數據(personalinfo,groups_designation)。所以我們有一個提供該screen.In,用戶選擇的組,然後分配個人ID和數據推送到groups_desig_category table.In這種情況下,我像映射
Personal.hbm.xml:-
<set name="empwthgrp" inverse="true" lazy="true" table="groups_desig_category">
<key>
<column name="pid" not-null="true" />
</key>
<many-to-many entity-name="com.aims.beans.DesignationGroupBean">
<column name="gid" not-null="true" />
</many-to-many>
</set>
Personal.java:-
/**
*
*/
private static final long serialVersionUID = 1L;
private int pid,deptno;
private String name,designation;
private Address address;
private Address permentaddress;
private Set famildtlslst;
private Set empwthgrp=new HashSet();
public Set getEmpwthgrp() {
return empwthgrp;
}
public void setEmpwthgrp(Set empwthgrp) {
this.empwthgrp = empwthgrp;
}
public Set getFamildtlslst() {
return famildtlslst;
}
public void setFamildtlslst(Set famildtlslst) {
this.famildtlslst = famildtlslst;
}
public Address getPermentaddress() {
return permentaddress;
}
public void setPermentaddress(Address permentaddress) {
this.permentaddress = permentaddress;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
GroupingDesig.hbm.xml: -
<class name="beans.DesignationGroupBean" table="groups_designation" proxy=beans.DesignationGroupBean">
<id name="gid" column="gid" type="java.lang.Integer">
<generator class="sequence"><param name="sequence">gid_seq</param> </generator>
</id>
<property name="gname" type="java.lang.String" column="gname" not-null="true" />
<property name="description" type="java.lang.String" column="description" not-null="true" />
<set name="grpwthemp" inverse="true" lazy="true" table="groups_desig_category">
<key>
<column name="gid" not-null="true" />
</key>
<many-to-many entity-name="com.aims.beans.Personal">
<column name="pid" not-null="true" />
</many-to-many>
</set>
</class>
DesignationGroupBean。 java: -
private int gid;
private String gname,description;
private Set grpwthemp=new HashSet();
public Set getGrpwthemp() {
return grpwthemp;
}
public void setGrpwthemp(Set grpwthemp) {
this.grpwthemp = grpwthemp;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
其實我試着session.saveOrUpdate(pBean)。但它沒有工作。可能會改變一對多和多對一而不是多對多的關係。我認爲它不適合這種情況。所以, ,在這種情況下如何處理?如果你使用反向工程,那麼它創建爲一對多和多對一的關係?爲什麼?請幫助我。
更新: -
我在一個一對多和多對一的一個很實現的關係數據庫冬眠,而它的許多一對多relation.then它的做工精細,並粘貼以下休眠映射文件,一個一對多的關係艦
GroupingDesig.hbm.xml: -
<set name="grpwthemp" inverse="true" lazy="true" table="groups_desig_category">
<key>
<column name="gid" not-null="true" />
</key>
<one-to-many class="com.aims.beans.GroupAssignment"/>
<!-- <many-to-many entity-name="com.aims.beans.Personal">
<column name="pid" not-null="true" />
</many-to-many>-->
</set>
Personal.hbm.xml
<set name="empwthgrp" inverse="true" lazy="true" table="groups_desig_category">
<key>
<column name="pid" not-null="true" />
</key>
<one-to-many class="com.aims.beans.GroupAssignment"/>
<!--
<many-to-many entity-name="com.aims.beans.DesignationGroupBean">
<column name="gid" not-null="true" />
</many-to-many>-->
</set>
AssigGroupingDesig.hbm.xml: -
<many-to-one name="personal" column="pid" class="com.aims.beans.Personal" not-null="true"></many-to-one>
<many-to-one name="desigdt" column="gid" class="com.aims.beans.DesignationGroupBean" not-null="true"></many-to-one>
時,將來到圖片的關係。我的船已經搜索到很多一對多的關係,例如在網頁即,.?
請幫幫我。 我的問題是什麼時候會來實時使用多對多關係船?
更新2: -
Thanks.Removing逆標籤及其工作fine.But我也不清楚哪些一代刪除query.Please的檢查日誌
/* load com.beans.Personal */ select personal0_.pid as pid0_, personal0_.name as name5_0_, personal0_.DEPTNO as DEPTNO5_0_, personal0_.designation as designat4_5_0_, personal0_.pddress1 as pddress5_5_0_, personal0_.pddress2 as pddress6_5_0_, personal0_.pcity as pcity5_0_, personal0_.pstate as pstate5_0_, personal0_1_.HomeAddress1 as HomeAddr2_7_0_, personal0_1_.HomeAddress2 as HomeAddr3_7_0_, personal0_1_.homecity as homecity7_0_, personal0_1_.homestate as homestate7_0_ from personalinfo personal0_, address personal0_1_ where personal0_.pid=personal0_1_.pid and personal0_.pid=?
delete collection com.beans.Personal.empwthgrp */ delete from groups_desig_category where pid=?
insert collection row com.beans.Personal.empwthgrp */ insert into groups_desig_category (pid, gid) values (?, ?)
爲什麼產生「從groups_desig_category刪除其中pid =?」。PLZ幫我
更新3: -
Yes.Iam使用session.get.becuase我關於一些強制性fields.that的有例外加載數據的原因,我加載然後將數據更新記錄
per=(Personal)session.get(Personal.class,new Integer(pBean.getPid()));
per.setEmpwthgrp(pBean.getEmpwthgrp());
session.saveOrUpdate(per);
感謝nicolae.Its工作fine.I有疑問,請在日的uDate公司2評論上述關於代「刪除」 query.Please幫我 – user1357722 2012-07-20 11:36:59
@ user1357722請問當你正在更新一個現有的個人時刪除?如果正在更新,請顯示正在進行更新的代碼塊(例如,如果您正在獲取個人通過加載(),則會顯示該代碼塊,然後在此情況下更改PersistentSet的多對多集合,或者使用一個不是Hibernate返回的集合)。 – 2012-07-20 12:09:16
再次感謝您。請檢查更新3評論。 – user1357722 2012-07-20 14:17:57