0

我有三个表,即个人信息、groups_designation、groups_design_category

  • 个人信息:pid(个人ID)
  • groups_designation:gid(组 id)
  • groups_design_category:gid,pid

实际上我在两个表中都有数据(个人信息,组_设计)。所以我们提供了一个屏幕。在那,用户选择组并分配个人ID,并将数据拉入groups_design_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)。但它不起作用。可能可以更改一对多和多对一而不是多对多关系。我认为它不适合这种情况。所以,在这种情况下如何处理?如果您使用逆向工程,那么它会创建为一对多和多对一的关系?为什么?请帮帮我。

更新:-

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

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>

个人.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 多对多

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

更新 2:-

谢谢。删除反向标签它工作正常。但我对删除查询的生成有疑问。请检查日志

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

为什么生成“从pid =的groups_desig_category中删除?”。请帮助我

更新 3:-

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

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

1 回答 1

1

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

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

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

于 2012-07-20T06:11:07.620 回答