本帖最后由 a11sos 于 2013-07-17 12:02:02 编辑

解决方案 »

  1.   

    我在想new 一对方对象放到这个集合里 可以不可以实现增加。。 删除呢。。
      

  2.   

    set cashcade配置了就可以的
      

  3.   

    例子:
    A.java实体代码package com.aaron.prj.inventory.vo;import java.util.Set;public class A { private int id;
    private String name; private Set b; public int getId() {
    return id;
    } public void setId(int id) {
    this.id = id;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public Set getB() {
    return b;
    } public void setB(Set b) {
    this.b = b;
    }}
    A.hbm.xml中详细配置信息:
    <?xml version="1.0" encoding="gb2312"?> 
    <!DOCTYPE hibernate-mapping 
    PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="com.aaron.prj.inventory.vo.A" table="inv_a"> <id name="id" column="aId">
    <generator class="native" />
    </id> <property name="name" column="aName" /> <set name="b" table="inv_a_b" cascade="save-update" inverse="false">
    <key column="a_id" not-null="true" />
    <many-to-many column="b_id" class="com.aaron.prj.inventory.vo.B" />
    </set> </class></hibernate-mapping>在hbm配置many-to-many按照如下方式即可: <set name="b" table="inv_a_b" cascade="save-update" inverse="false">
    <key column="a_id" not-null="true" />
    <many-to-many column="b_id" class="com.aaron.prj.inventory.vo.B" />
    </set>
      

  4.   

    谢谢
    那我要在代码里 实现删除 怎么实现。。对这个Set集合如何操作 增加如何操作 
      

  5.   

    main如何增加操作(删除操作.delete()方法即可)package com.aaron.prj.inventory.action;import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;import com.aaron.prj.inventory.vo.A;
    import com.aaron.prj.inventory.vo.B;public class A_BSaveAction { public static void main(String args[]) { // Initial cfg file.
    Configuration cfg = new Configuration().configure(); // Create session factory.
    SessionFactory sf = cfg.buildSessionFactory(); // Get a connection from sf.
    Session s = sf.openSession(); // Open transaction.
    Transaction t = s.beginTransaction(); // Entity set value, and it's the cache state.
    A a1 = new A();
    a1.setName("a1"); A a2 = new A();
    a2.setName("a2"); B b1 = new B();
    b1.setName("b1"); B b2 = new B();
    b2.setName("b2"); try { // Perform save operation.
    s.save(a1);
    s.save(a2);
    s.save(b1);
    s.save(b2);// A_B ab1 = new A_B();
    // ab1.setA(a1);
    // ab1.setB(b1);
    //
    // A_B ab2 = new A_B();
    // ab2.setA(a1);
    // ab2.setB(b2);
    //
    // A_B ab3 = new A_B();
    // ab3.setA(a2);
    // ab3.setB(b1);
    //
    // A_B ab4 = new A_B();
    // ab4.setA(a2);
    // ab4.setB(b2);
    //
    // s.save(ab1);
    // s.save(ab2);
    // s.save(ab3);
    // s.save(ab4);

    // Commit transaction.
    t.commit(); } catch (HibernateException e) { e.printStackTrace();
    t.rollback(); } finally { // Close connection, and it's the free state.
    s.close();
    sf.close();
    }
    }
    }
      

  6.   

    多对多 维护关系 的增加 就是对实体类里的SET