简体   繁体   English

如何使用Hibernate中的现有数据处理多对多关系

[英]How to handle Many-To-Many Relation with existed data in Hibernate

I had three table ie,personalinfo,groups_designation,groups_desig_category 我有三个表,即,personalinfo,groups_designation,groups_desig_category

  • personalinfo:pid(personal id) personalinfo:pid(个人ID)
  • groups_designation:gid(group id) groups_designation:gid(组ID)
  • groups_desig_category:gid,pid groups_desig_category:gid,pid

Actually I have data in both table's (personalinfo,groups_designation).So we have provide one screen.In that,The user selects the group and assign personal id and the data pulled into groups_desig_category table.In this scenario,i mapped like 实际上,我在两个表的个人信息中都有数据(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:- 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:- 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:- 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;
}

Actually I trying session.saveOrUpdate(pBean).But its not working.May be can change one-to many and many-to-one instead of many-to-many relation.I think it is not suitable in this scenario.So,How to handle in this scenario?.If you using reverse engineering then it created as one-to-many and many-to-one relation? 实际上我正在尝试session.saveOrUpdate(pBean)。但是它不起作用。可能会改变一对多和多对一的关系,而不是多对多的关系。我认为这不适合这种情况。在这种情况下该如何处理?如果使用逆向工程,那么它将创建为一对多和多对一关系? why?.Please help me. 为什么?请帮助我。

Update:- 更新:-

I am implemented in one-to-many and many-to-one relation hibernate whereas in database its many-to-many relation.then Its working fine and below pasted the hibernate mapping files with one-to-many relation ship 我是在一对多和多对一的休眠状态下实现的,而在数据库中它是多对多的关系。那么它的工作原理如下:

GroupingDesig.hbm.xml:- 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 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:- 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>

When will be came picture the relation ship?.I have search many-to-many relation example's in web ie,. 什么时候来图片关系船?。我在Web上搜索了多对多实例。

Mykong many-to-many 湄公河多对多

Please help me. 请帮我。 My Question is when will be came/used many-to-many relation ship in real time?. 我的问题是何时实时出现/使用多对多关系船?

Update 2:- 更新2:-

Thanks.Removing the inverse tag its working fine.But i have doubt regarding generation of deleting the query.Please check the logs 谢谢。删除反向标签的工作正常。但我怀疑删除查询的生成。请检查日志

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

why generating the "delete from groups_desig_category where pid=?".Plz help me 为什么生成“从groups_desig_category的pid =?中删除?”。请帮助我

Update 3:- 更新3:-

Yes.Iam loading the data using session.get.becuase i got exception regarding the some of mandatory fields.that is reason i loaded the data then update the records 是的,我使用session.get加载数据,因为我遇到了一些必填字段的异常。这就是我加载数据然后更新记录的原因

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

In your many-to-many mappings, you set both of them to inverse . 在多对多映射中,将它们设置为inverse You need to choose one entity that will own the relationship - for that one, in the mapping, you will remove the inverse="true" setting. 您需要选择一个拥有该关系的实体-为此,在映射中,您将删除inverse="true"设置。 That will be the entity that, when saved or updated, will persist the person to group relationship. 该实体将在保存或更新后将持久化人员与组的关系。

Since in your question you posted saveOrUpdate(pBean) , and I assume pBean is Personal entity, then you need to remove the inverse="true" setting in Personal.hbm.xml . 因为在你的问题你贴saveOrUpdate(pBean)我认为pBeanPersonal的实体,那么你需要删除inverse="true"在制定Personal.hbm.xml

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM