如何在Hibernate中处理与现有数据的多对多关系

时间:2012-07-19 11:04:31

标签: java hibernate many-to-many hibernate-mapping

我有三张表,即personalinfo,groups_designation,groups_desig_category

  • personalinfo:pid(个人身份证)
  • groups_designation:gid(组ID)
  • groups_desig_category:GID,PID

实际上我在两个表中都有数据(personalinfo,groups_designation)。所以我们提供了一个屏幕。用户可以选择该组并将个人ID和数据分配到groups_desig_category表中。在这种情况下,我映射为

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)。但它不能正常工作。可以改变一对多和多对一而不是多对多的关系。我认为它不适合这种情况那么,如何处理这种情况?。如果你使用逆向工程,那么它创建为一对多和多对一的关系?为什么?。请帮帮我。

更新: -

我实现了一对多和多对一关系的休眠,而在数据库中实现了它的多对多关系。然后它的工作正常并在下面粘贴了一对多关系的hibernate映射文件

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>

何时会出现关系船的图片?我在网上搜索多对多关系的例子,即。

Mykong many-to-many

请帮帮我。我的问题是什么时候会实时出现/使用多对多关系?

更新2: -

Thanks.Removing逆标记它的工作正常。但我怀疑删除查询的生成。请检查日志

 /* 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 (?, ?)

为什么要生成“从group_desig_category删除pid =?”。。Plz帮帮我

更新3: -

是的。我使用session.get.becuase加载数据我对一些必填字段有异常。这就是我加载数据然后更新记录的原因

          per=(Personal)session.get(Personal.class,new Integer(pBean.getPid()));
    per.setEmpwthgrp(pBean.getEmpwthgrp());
    session.saveOrUpdate(per);

1 个答案:

答案 0 :(得分:1)

在您的多对多映射中,您将两者设置为反向。您需要选择一个拥有关系的实体 - 对于该实体,在映射中,您将删除inverse="true"设置。这将是一个实体,在保存或更新时,将使该人员保持团体关系。

由于您在问题中发布了saveOrUpdate(pBean),并且我认为pBeanPersonal实体,因此您需要删除inverse="true"中的Personal.hbm.xml设置。

参考文档中的更多信息:http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/associations.html#assoc-bidirectional-join-m2m